I have this jQuery and I'm changing styles in it but I've heard that the correct way to do it is to create a separate style and just replace classes with jQuery. Can you explain me how to do it correctly:
$('.corpo_buttons_asia').click(function() { $('.info').css('visibility', 'hidden'); $('.info2').css('visibility', 'visible'); $(this).css('z-index', '20'); $(this).css('background-color', 'rgb(23,55,94)'); $(this).css('color', '#FFF'); $('.corpo_buttons_global').css('background-color', 'rgb(197,197,197)'); $('.corpo_buttons_global').css('color', '#383838'); }); $('.corpo_buttons_global').click(function() { $('.info').css('visibility', 'visible'); $('.info2').css('visibility', 'hidden'); $(this).css('background-color', 'rgb(23,55,94)'); $(this).css('color', '#FFF'); $('.corpo_buttons_asia').css('z-index', '2'); $('.corpo_buttons_asia').css('background-color', 'rgb(197,197,197)'); $('.corpo_buttons_asia').css('color', '#383838'); });
So instead of using .css() all the time I can create another class and just replace it with jQuery.
To replace a class with another class, you can remove the old class using jQuery's . removeClass() method and then add the new class using jQuery's . addClass() method.
We can replace HTML elements using the jQuery . replaceWith() method. With the jQuery replaceWith() method, we can replace each element in the set of matched elements with the provided new content and return the set of elements that were removed.
The removeClass() method removes one or more class names from the selected elements. Note: If no parameter is specified, this method will remove ALL class names from the selected elements.
To change all classes for an element:document. getElementById("MyElement"). className = "MyClass"; (You can use a space-delimited list to apply multiple classes.)
To do this efficiently using jQuery, you can chain it like so:
$('.theClassThatsThereNow').addClass('newClassWithYourStyles').removeClass('theClassThatsTherenow');
For simplicities sake, you can also do it step by step like so (note assigning the jquery object to a var isnt necessary, but it feels safer in case you accidentally remove the class you're targeting before adding the new class and are directly accessing the dom node via its jquery selector like $('.theClassThatsThereNow')
):
var el = $('.theClassThatsThereNow'); el.addClass('newClassWithYourStyles'); el.removeClass('theClassThatsThereNow');
Also (since there is a js tag), if you wanted to do it in vanilla js:
For modern browsers (See this to see which browsers I'm calling modern)
(assuming one element with class theClassThatsThereNow
)
var el = document.querySelector('.theClassThatsThereNow'); el.classList.remove('theClassThatsThereNow'); el.classList.add('newClassWithYourStyleRules');
Or older browsers:
var el = document.getElementsByClassName('theClassThatsThereNow'); el.className = el.className.replace(/\s*theClassThatsThereNow\s*/, ' newClassWithYourStyleRules ');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With