Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add and remove class active on button with jquery onclick function

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.

like image 701
Giulio Bambini Avatar asked Apr 21 '15 17:04

Giulio Bambini


People also ask

How do you change a class when a button is clicked?

className = "myclass"; Example 1: In this code change the class of the button from “default” to “changedClass” using the onclick event which in turn changes the background color of the button from RED to GREEN.

How add and remove class in jQuery after some time?

If you have it applied to an element but want to remove it from an element after a designated amount of time has passed, you can do so by using jQuery's . setTimeout() method. var header = $('#header'); header. addClass('blue'); setTimeout(function() { header.

How add or remove a class in jQuery?

jQuery Manipulating CSSaddClass() - Adds one or more classes to the selected elements. removeClass() - Removes one or more classes from the selected elements. toggleClass() - Toggles between adding/removing classes from the selected elements.


2 Answers

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;
}
like image 111
Marcin Avatar answered Sep 28 '22 17:09

Marcin


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

like image 29
kosmos Avatar answered Sep 28 '22 17:09

kosmos