2

I've got the following list of semibuttons loaded using javascript:

        var html ='<ul class="nav well-tabs well-tabs-inverse mb10" id="users">';

        html +='<li class="active"><a id="'+this.my.user+'" data-toggle="tab_'+self.my.id+'" class="pestaña">'+this.my.user+'</a></li>';
        var users = this.my.community_users;
        for (i=0;i<users.length;i++) {
            if (users[i].user != this.my.user)
            html +='<li><a id="'+users[i].user+'" data-toggle="tab_'+self.my.id+'" class="pestana">'+users[i].user+'</a></li>';
        };
        html +='</ul>';
        $(html).appendTo("#Dashboard");

Note, that the first item in the list is active. I am getting something like this:

image

Ok, now i code he onclick event to do something when a button is clicked:

$(document).on('click', 'a[data-toggle=tab_'+self.my.id+']', function(e){
      // whatever here
});

What I need now is to set active the tab being clicked and set inactive the tab that was active. How can I access both elements to addclass and removeclass active?

4
  • 4
    var html += is basically wrong. Should be var html =. Or define it before concatenating using var html = ""; Commented Apr 19, 2015 at 20:06
  • Could you setup fiddle? Commented Apr 19, 2015 at 20:07
  • @Egidi even if corrected now, your question cannot be reproduced in a manner that one could play with. So... please show more. Commented Apr 19, 2015 at 20:10
  • FYI, you are using two different classes, pestaña and pestana Commented Apr 19, 2015 at 20:20

3 Answers 3

3

You could use following logic:

$(document).on('click', '#users li:not(.active)', function () {
    $('#users').find('li.active').add(this).toggleClass('active');
});
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this might work. Basically remove the .active class from everything but the element you clicked on. The add the .active class to the element clicked on.

$(document).on('click', 'a[data-toggle=tab_'+self.my.id+']', function (e) {
    $('a[data-toggle=tab_'+self.my.id+']').not(this).removeClass('active');
    $(this).addClass('active');
});

Comments

0

I would remove the 'active' class from all the list items first, then add it back to just the only that was clicked.

$(document).on('click', 'a[data-toggle=tab_'+self.my.id+']', function (e) {
    $('#users .active').removeClass('active');
    $(this).addClass('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.