I know I can get all checked checkboxes on a page using this:
$('input[type=checkbox]').each(function () {
var sThisVal = (this.checked ? $(this).val() : "");
});
But I am now using this on a page that has some other checkboxes that I don't want to include. How would I change the above code to only look at checked checkboxes that have a certain class on them?
An example to demonstrate.
:checkbox
is a selector for checkboxes (in fact, you could omit theinput
part of the selector, although I found niche cases where you would get strange results doing this in earlier versions of the library. I'm sure they are fixed in later versions)..class
is the selector for element class attribute containingclass
.This would get all checkboxes of the class name "yourClass". I like this example since it uses the jQuery selector checked instead of doing a conditional check. Personally I would also use an array to store the value, then use them as needed, like:
Obligatory
.map
example:Simple way to get all of values into an array
See jQuery class selectors.