Skip to content Skip to sidebar Skip to footer

Jquery Checkbox Selection And Change Class

I have a form like this:

Solution 1:

If I've understood your question correctly, then what you're trying to do is bind a click event handler to all the checkboxes, and add/remove a class to parent label of the one that's been clicked. If that's correct, then you can do this:

$("input[type='checkbox']").change(function() {
   $(this).closest("label").toggleClass("someClass"); 
});

The toggleClass method removes the need to check whether or not the checkbox is checked. You can pass in a second argument to show whether or not the class should be added or removed, so if some of your checkboxes start off already checked, you may want to use that:

$("input[type='checkbox']").change(function() {
   $(this).closest("label").toggleClass("someClass", this.checked); 
});

Here's a working example of the above code.

Post a Comment for "Jquery Checkbox Selection And Change Class"