I have a simple div
with multiple checkboxes in it. I want to look for any change in the checkbox to make an ajax query.
This code is not able to see the changes in those checkboxes. What is the right way to do it?
$(document).on('change', '#listfilters checkbox', function(e) {
if(this.checked) {
alert("changed");
// checkbox is checked
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="listfilters">
<p>
<input type="checkbox" id="showall" name="showall" value=0>
<label for="showall">Show All</label>
</p>
<p>
<input type="checkbox" id="showfl" name="showfl" value=0>
<label for="showfl">Filtered</label>
</p>
</div>
2
Answers
You need to qualify the checkbox part of the selector. Change it to
#listfilters [type=checkbox]
Instead of
document
, you can select directly yourdiv
for a quick/faster selector.