9
I’m learning jQuery and I don’t always know exactly how to develop the code more cleanly, semantically, I’ll end up learning. How could I improve this code?
$("#click_apoio").on('click', function( evento ){
evento.preventDefault();
if( $("#institucional").hasClass('desativado') ){
$("#institucional").removeClass('desativado');
$("#institucional").addClass('ativado');
$("#parceiros").removeClass('ativado');
$("#parceiros").addClass('desativado');
}
});
You can also think of another structure... you are using two classes: enabled and disabled... if you used only one class, you would use instead of removeClass and addClass.. the toggleClass method... and would adapt the css to suit this structure...
– Pedro Laini
I agree with what Peter said !!!
– Marcos Vinicius
Dude, sometimes making code cleaner doesn’t mean having less line amount, it’s making it legible. Your code is good and to be understood. Another important factor is to check if you have any repeated code, to separate into functions
– Erico Souza
@Ericosouza is exactly right! As I commented below, there is nothing "unprofessional" in this code, it is good, can perfectly understand what you are doing!
– user3603
Thank you all for finding the issue interesting enough for a short debate.
– Marcos Vinicius
A question... is there the need for jQuery in its function? Was that question only at the level of learning? Because in my view, I could very well achieve the same goal only with Avascript
– MarceloBoni
So @Marcelobonifazio I am part of the group of people who learn jQuery without really knowing Javascript. Javascript study but not to the point of prioritizing it over jQuery. :)
– Marcos Vinicius
Yes, it is valid, but keep in mind that using jQuery affects the performance of the page, even if little, although with jQuery you have access to many useful things that only with pure Avascript becomes more complicated to do
– MarceloBoni
This question is being discussed in the target: http://meta.pt.stackoverflow.com/questions/4210/eu-sou-f%C3%A3-but-unfortunately-this-tool-inhibits-participates%C3%A7%C3%B5es-porqu%C3%AA-tem-qu
– Jorge B.
As it is I did not vote to close, alias withdrew the vote. Although I still do not like most of the questions.
– Jorge B.