How can I change the id in javascript?

Discussion in 'Web Design' started by benjamin.morgan, Jul 7, 2012.

to remove all ads.
  1. CaldwellYSR

    CaldwellYSR Member

    Messages:
    658
    Yep, the beautiful thing about jquery is you can still do all the same things you would do with core JS... you just have extra stuff that you can also do.
     
  2. PixelPusher

    PixelPusher Super Moderator Staff Member

    Messages:
    1,077
    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>
    
     

Share This Page