Question about checking the state of a checkbox class

I will try to keep this question simple. Can I assign a class to a number of different checkboxes and use JQuery to do something when one of those checkboxes is checked? Searching on the web I found documentation on capturing name: $('input[name=foo]').is(':checked')

but when I change the name attribute for the attr class it doesn't work! How can I set an event if a checkbox with that specific class is checked? Please help me! Thanks to

+2


a source to share


2 answers


Use hasClass

to check if a specific element is assigned to a specific class, e.g .:

$(document).ready(function() {
    $("input[name=something]:checkbox").click(function() {
        if($(this).is(":checked") && $(this).hasClass("foo")) {
           // the checkbox has been checked and has a class of foo
        }
    });
});

      



Try demo here

+3


a source


You can get the status of all checkboxes in one go by putting the same name on all checkboxes.



0


a source







All Articles