4

I have a data filtration function in my app, where users can use checkboxes and dropdowns to select how they want to filter the data.

I am using the bootstrap checkboxes:

<div class="btn-group" data-toggle="buttons">
 <label class="btn btn-primary active">
  <input type="checkbox" autocomplete="off" checked> Checkbox 1 (pre-checked)
 </label>
 <label class="btn btn-primary">
  <input type="checkbox" autocomplete="off"> Checkbox 2
 </label>
 <label class="btn btn-primary">
  <input type="checkbox" autocomplete="off"> Checkbox 3
 </label>
</div>

I have a clear filter buttons which clears all the users inputs from the filters, resets all checkboxes, all drop downs, all text fields.

I was able to reset drop downs like this:

// sectorPicker is drop down
document.getElementById('sectorPicker').reset();

Below is the image of my checkboxes:

enter image description here

But I can't figure our how to reset the Bootstrap checkboxes on the screen to uncheck all checkboxes.

Any thoughts?

4 Answers 4

10

this will remove the checked property and also active class from its parent label

$(":checkbox").prop('checked', false).parent().removeClass('active');
Sign up to request clarification or add additional context in comments.

1 Comment

I needed a slightly different approach, maybe due to bootstrap version: $("#checkbox").prop('checked', false).parent().removeClass('checked');
3

Try this, It will loop through all selected checkboxes and make them un checked. Or you can give each checkbox a class attr and then make loop over it.

     $( "input:checked" ).each(function(){
           $(this).removeAttr("checked");
           $(this).parent().removeClass("btn btn-primary");
            $(this).parent().removeClass('active');
           $(this).parent().addClass("btn btn-default");
     });

//Way two:

<div class="btn-group" data-toggle="buttons">
 <label class="btn btn-primary active">
  <input type="checkbox" class="mychk" autocomplete="off" checked> Checkbox 1 (pre-checked)
 </label>
 <label class="btn btn-primary">
  <input type="checkbox" class="mychk" autocomplete="off"> Checkbox 2
 </label>
 <label class="btn btn-primary">
  <input type="checkbox" class="mychk" autocomplete="off"> Checkbox 3
 </label>
</div>

$(".mychk").each(function(){
 $(this).removeAttr("checked");
});

2 Comments

Thank you for the answer. I tried this but my checkboxes still remain highlighted. If you check my question I've posted an image. You can see they are highlighted as Blue even after clearing them.
That is your button class, you need to remove btn btn-primary class from label element, see my updated answer
2

As far as i know bootstrap checkboxes use properties instead of attributes.

$("[type='checkbox']").prop("checked", false) something like that should work.

Comments

1
$('#You_id_checkbox').removeAttr("checked");

Comments

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.