How can I change the id in javascript?

PixelPusher

Super Moderator
Staff member
As others have mentioned, I would target the "class" attribute over "id", simply because you have built in methods for handling classes.

Consider the following: .addClass(); .removeClass(); .toggleClass(); .toggle(); .click();

Code:
$('#switch').click(function() { 
   $('body').find('div.red').toggleClass('red').toggleClass('blue');
});
HTML:
<body>
   <a href="#" id="switch">Switch Color</a>
   <div class="red">
       <!-- Element contents -->
   </div>
</body>
 
Top