I’m sure what I’m trying to do is quite simple however I require some help.
I have three radio buttons;
- active
- inactive
- not found
Id like to change the (Bootstrap v3) class of the buttons when they are clicked. They all have a default class of btn-default
. However when clicked i’d like then to have the following classes;
- active (
btn-success
) - inactive (
btn-warning
) - not found (
btn-danger
)
Only one should be selected at any one time, and only one should be coloured at any one time.
My code so far is below, I think I’m nearly there. If somebody could offer some advice that would be great.
Here is a link to my jsfiddle.
$(document).ready(function() {
$('#option1').change(function() {
$(this).removeClass("btn-default").addClass("btn-success");
});
$('#option2').change(function() {
$(this).removeClass("btn-default").addClass("btn-warning");
});
$('#option3').change(function() {
$(this).removeClass("btn-default").addClass("btn-danger");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default">
<input type="radio" name="options" id="option1" value="1" autocomplete="off">Active
</label>
<label class="btn btn-default">
<input type="radio" name="options" id="option2" value="2" autocomplete="off">Inactive
</label>
<label class="btn btn-default">
<input type="radio" name="options" id="option3" value="3" autocomplete="off">Not Found
</label>
</div>
7
Answers
Man, first of all, you should include bootstrap.js after jquery.js
Second, your selector selects child element, but you need to change class of parent. So you need to use
parent()
hereCode snippet below:
The code you have is not changing the classes because you are targetting the wrong elements. However, the other answers failed to take into account that you want only one to be coloured at any one time. Simply include the removing classes and adding the default classes back on changes of each.
Try this:
The issue is you’r binding your change event handler to the
input
‘s, but the<label>
‘s are the ones that are styled withbtn-default
.Change the references to
$(this)
to$(this).parent()
and it will work as intended.Remove all classes currently applied, once that’s done, figure out which button has been clicked and apply that options CSS. In regards to why the CSS wasn’t changing, you was not targeting the labels in where the btn-“insert bootstrap button styling here” was applied, by calling parent(), you go up to the level which controlled the CSS.
In addition, generalise the removal:
To get only one to be activate, you must remove class from other button. this is the code
You can have a click event on
btn-default
instead and have only one handler:JSFiddle
You could use
Custom Data Attribute
like this