Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

JQuery checkbox checked state changed event

I want an event to fire client side when a checkbox is checked / unchecked:

$('.checkbox').click(function() {
if ($(this).is(':checked')) {
// Do stuff
}
});


Basically I want it to happen for every checkbox on the page. Is this method of firing on the click and checking the state ok?

I'm thinking there must be a cleaner jQuery way. Anyone know a solution?
by

3 Answers

akshay1995
Bind to the change event instead of click. However, you will probably still need to check whether or not the checkbox is checked:

$(".checkbox").change(function() {
if(this.checked) {
//Do stuff
}
});

To get all checkboxes you have a couple of options. You can use the :checkbox pseudo-selector:

$(":checkbox")

Or you could use an attribute equals selector:

$("input[type='checkbox']")
kshitijrana14
$(document).ready(function () {
$(document).on('change', 'input[Id="chkproperty"]', function (e) {
alert($(this).val());
});
});
pankajshivnani123
Just another solution

$('.checkbox_class').on('change', function(){ // on change of state
if(this.checked) // if changed state is "CHECKED"
{
// do the magic here
}
})

Login / Signup to Answer the Question.