Joseph
Joseph

Reputation: 209

Question regarding checking the state of a checkbox class

I'll try and make this question simple. Can I assign a class to a series of different checkboxes, and use Jquery to do something when any one of those checkboxes is checked? Searching around on the internet I have found documentation on grabbing the name: $('input[name=foo]').is(':checked') but when I swap out the name attribute for the class attr, it won't work! How can I set an event to occur if any of the checkboxes with this certain class is checked? Please help me out! Thanks

Upvotes: 2

Views: 67

Answers (2)

karim79
karim79

Reputation: 342635

Use hasClass to test whether or not a particular element has been assigned a certain 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 a demo here.

Upvotes: 3

Kangkan
Kangkan

Reputation: 15571

You can get the status of all the checkbox at a single go by putting same name to all the checkboxes.

Upvotes: 0

Related Questions