6

I'm actually using this button group structure with bootstrap. Now I need to add class active to each button when I click on it. And remove from it when I click on another button.

This is my HTML structure, is something like that:

<body>
    <div id="header">
        <div class="logo">Prova<span class="sec-logo">toscana</span>15</div>
        <div class="bottoni">
            <div class="btn-group" role="group" aria-label="...">
                <button type="button" class="btn btn-default" id="b1">12345</button>
                <button type="button" class="btn btn-default" id="b2">12345</button>
                <button type="button" class="btn btn-default" id="b3">12345</button>
                <button type="button" class="btn btn-default" id="b4">12345</button>
                <button type="button" class="btn btn-default" id="b5">12345</button>
                <button type="button" class="btn btn-default" id="b6">12345</button>
            </div>
        </div>
    </div>
</body>

Someone who could help me? Thanks.

2
  • What have you tried so far? Please provide sample code with details of the problem(s) you're encountering. Commented Apr 21, 2015 at 18:17
  • try this jsfiddle.net/q85r1xxq/2 Commented Apr 21, 2015 at 18:27

4 Answers 4

5

if .active class should not be removed from active button after clicking on it please use addClass instead of toggelClass

$("#header .btn-group[role='group'] button").on('click', function(){
    $(this).siblings().removeClass('active')
    $(this).addClass('active');
})

jsfiddle example

it is also good practice to narrow buttons selection, I used #heade id and .btn-group[role='group'] which makes script applied only on buttons inside all button groups iside <div id="header"></div>

and here you have .active class definition:

.btn.active{
    background-color: red;
}
Sign up to request clarification or add additional context in comments.

Comments

3

You are looking for something like:

$('.btn').on('click', function(){
    $(this).siblings().removeClass('active'); // if you want to remove class from all sibling buttons
    $(this).toggleClass('active');
});

Check jsFiddle

1 Comment

If the jsFiddle works... (and it works actually), I think it works (you can test in opening the console, at elements tab). Maybe are you looking for another workaround?
0

You should use a combination of .each() and .click() here. So it will target every button in stead of only the first

$('#header .btn').each(function(){
    $(this).click(function(){
        $(this).siblings().removeClass('active'); // if you want to remove class from all sibling buttons
        $(this).toggleClass('active');
    });
});

Comments

0

I hope this will help you because it works perfectly for me

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $(".btn").each(function(){
       $(this).click(function(){
          $(this).addClass("active");
          $(this).siblings().removeClass("active");
       });
    });
});
</script>

Try this one

2 Comments

jQuery's class selector refers to all elements with specified class, so you are doing a loop which is no needed
In that case we can specify it in a inherited way right but the loop is required bcoz it needs to check whether it is clicking on the current button and for all its siblings the class needs to be removed

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.