I have a problem with my code using the bootstrap plugin. I have some radio buttuns in the following code:
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="options" id="option1" autocomplete="off" checked> Radio 1 (preselected)
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option2" autocomplete="off"> Radio 2
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option3" autocomplete="off"> Radio 3
</label>
</div>
I want to put a button to reset the selection. I've tried a reset button (<button type="reset">
), jQuery commands, but all fail.
In the Bootstrap API I found an explanation: http://getbootstrap.com/javascript/#buttons-checkbox-radio
that says:
Visual checked state only updated on click If the checked state of a checkbox button is updated without firing a click event on the button (e.g. via or via setting the checked property of the input), you will need to toggle the .active class on the input's label yourself.
Please someone could help me find a solution to my problem?
Thanks, Gabriel.
First of all, you need to remove active class from label tag, then make the properties checked element on radio into false state, try this simple example below :
$('button').click(function () {
$('.btn-group').find('label').removeClass('active')
.end().find('[type="radio"]').prop('checked', false);
});
DEMO
$("button").on('click', function() {
$("input:radio").prop('checked', false);
$("input:radio").closest("label").removeClass("active");
})
This jQuery code should help you. When you click a button, it removes the checked property from all radios and toggles the class of the label as the API says
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With