Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Radio Button uncheck by jQuery

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.

like image 658
Gabriel Vargas Avatar asked Aug 28 '15 01:08

Gabriel Vargas


2 Answers

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

like image 96
Norlihazmey Ghazali Avatar answered Nov 01 '22 16:11

Norlihazmey Ghazali


$("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

like image 26
DannyPhantom Avatar answered Nov 01 '22 17:11

DannyPhantom