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?
$('.theClass:checkbox:checked')
will give you all the checked checkboxes with the classtheClass
.I'm not sure if it helps for your particular case, and I'm not sure if in your case, the checkboxes you want to include only are all part of a single form or div or table, but you can always select all checkboxes inside a specific element. For example:
Then, using the following jQuery, it ONLY goes through the checkboxes within that UL with id="selective":
I know this has a bunch of great answers on this question already but I found this while browsing around and I find it really easy to use. Thought I'd share for anyone else looking.
HTML:
jQuery:
Reference: Easiest "Check All" with jQuery
If you need to get the value of all checked checkboxes as an array:
You can use something like this:
HTML:
JQuery:
It will choose values of 1 and 3.
http://www.jqueryfaqs.com/Articles/Get-values-of-all-checked-checkboxes-by-class-name-using-jQuery.aspx