1

I've the following html code

<ul class='selectul'>
    <li class='active'><a href='?sortby=views'>by Popularity.</a></li>
    <li><a href='?sortby=recency'>by Recency.</a></li>
    <li><a href='?sortby=alphabet'>Alphabetically.</a></li>
</ul>

When any <li> element is clicked, I want to add class='active' to it. None of its siblings is then allowed to have this class. I tried to do this with the following jQuery code

(function(){
    $('.selectul li').on('click',function(){
        $(this).addClass('active');
        $(this).siblings().removeClass('active');
    });
}())

but it doesn't seem to do anything. I'm just getting started with jQuery so I apologize if this question is rather basic.

4
  • 3
    You should use document ready handler instead of a self-invoking function. Commented Apr 12, 2013 at 12:51
  • 1
    Your code looks fine, i suspect you need to wrap your code in a document ready event - api.jquery.com/ready Commented Apr 12, 2013 at 12:52
  • If you are just getting started you should follow this tutorial very closely: docs.jquery.com/Tutorials:Getting_Started_with_jQuery. Commented Apr 12, 2013 at 12:52
  • 1
    Also, you can chain the functions $(this).addClass('active').siblings().removeClass('active'); Commented Apr 12, 2013 at 12:59

4 Answers 4

5

The script is all right. The problem is somewhere else. Once you click the link, the document reload and you do not notice the change of classes.

Try:

(function(){
  $('.selectul li').on('click',function(){
    $(this).addClass('active');
    $(this).siblings().removeClass('active');
    return false;
  });
}());

return false interrupts the Click-Event and prevent the reload of the document.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks, that did the trick! However, now my query string doesn't change anymore when I click on the options. And that is a bit of a problem...
If you are sending a request anyway, then change the active-class on the server side ;-)
do you mean I should have PHP check for the query string and modify the HTML according to it?
Exactly, for example: <? if ($_GET['sortby'] == 'views'): ?>class="active"<? endif; ?>
Clear. I'll see if that'll accomplish what I'm looking for!
1

Your code looks good, and I have verified it working @ jsfiddle.net

http://jsfiddle.net/kEfyT/1/

$(document).ready(function(){
    $('.selectul li').on('click',function(){
        $(this).addClass('active');
        $(this).siblings().removeClass('active');
    });
});

2 Comments

thank you, I tried that but it still seems to do nothing. I know the html source code is not supposed to change but in the Google Chrome Developer tools, I should see this class being added, right? It still isn't... What else could be the problem?
The problem is that the page reloads on clicking, undoing the jQuery effect :-(
0

Try waiting for a document ready event as the elements might not be on the page yet.

$(function(){
    $('.selectul li').on('click',function(){
        $(this).addClass('active');
        $(this).siblings().removeClass('active');
    });
});

Comments

0

I think you should code like this

http://jsfiddle.net/3WBvL/2/

$(document).ready(function(){
    $('.selectul li').on('click',function(){

        $(this).addClass('active');
        $(this).siblings().removeClass('active');
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.