Jquery adding and removing class dynamically

Posted by user244394 on Stack Overflow See other posts from Stack Overflow or by user244394
Published on 2011-01-13T03:40:37Z Indexed on 2011/01/13 3:54 UTC
Read the original article Hit count: 198

Filed under:
|

I am trying to add the class"selected" when a link is clicked and when the user click on the next link , I want to remove the previously "selected" class and add "selected" to the link clicked..

-Thanks in advance

$(document).ready(function() {
$('.news a').click(function(){
  $(this).addClass("selected");
});
});

<div class="news-w">
            <div class="news" id="getnews-1">
            <a href="#" >topic</a>
            </div>
            <div class="news" id="getnews-2">
            <a href="#">topic</a>
            </div>      
            <div class="news" id="getnews-3">
            <a href="#" >topic</a>
            </div>      
            <div class="news" id="getnews-4">
            <a href="#">topic</a>
            </div>      
            <div class="news" id="getnews-5">
            <a href="#">topic</a>
            </div>                      
    </div>

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about class