Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery replace one class with another

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.

like image 254
user1047517 Avatar asked Nov 23 '11 22:11

user1047517


People also ask

How can I replace one class with another in 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.

Can we replace in jQuery?

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.

What is removeClass in jQuery?

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.

How do you change the class of an element?

To change all classes for an element:document. getElementById("MyElement"). className = "MyClass"; (You can use a space-delimited list to apply multiple classes.)


1 Answers

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 '); 
like image 125
Rooster Avatar answered Sep 24 '22 13:09

Rooster