2

I have three links on which I manually set classes: One defaults to .active and the others default to .inactive. I have some jQuery that I'm using to toggle the class of the links when they are clicked for CSS purposes. This works for the .inactive links - they switch to .active. However it doesn't work for the default .active link. If I click on the default .active link it stays inactive.

Default load:

Link 1 - Active Link 2 - Inactive Link 3 - Inactive

Clicking Link 2:

Link 1 - Inactive Link 2 - Active Link 3 - Inactive

Clicking Link 1 from Link 2:

Link 1 - Inactive Link 2 - Active Link 3 - Inactive

How can I complete this cycle so it works?

Here is the HTML I'm generating:

<ul id="infoContainer">
  <li><a href="/profiles/1/profile_cred" class="active" data-remote="true">Cred</a></li>
  <li><a href="/profiles/1/profile_about" class="inactive" data-remote="true">About</a></li>
  <li><a href="/profiles/1/profile_questions" class="inactive" data-remote="true">Questions</a></li>
</ul>

The jQuery I'm using:

$(function(){
    var sidebar=$('#sidebar');
    sidebar.delegate('a.inactive', 'click',function(){
        sidebar.find('.active').toggleClass('active inactive');
        $(this).toggleClass('active inactive');
    });
});
2
  • What do you mean by saying the default active stays inactive? Commented Oct 29, 2011 at 14:02
  • 1
    It seems like it works fine: jsfiddle.net/pimvdb/WuqNG. Clicking link 2 and then 1 again switches classes correctly. Commented Oct 29, 2011 at 14:14

1 Answer 1

2

If I do get you correct, your selector is too strict: handling only the inactive links.
Tried the following?

$(function(){
    var sidebar=$('#sidebar');
    sidebar.delegate('a', 'click',function(){
        sidebar.find('.active').toggleClass('active inactive');
        $(this).addClass('active').removeClass('inactive');
    });
});
Sign up to request clarification or add additional context in comments.

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.