leora
leora

Reputation: 196499

what is the right jquery selector syntax to get all checkboxes with a certain class name?

i have jquery code that loop through all checkboxes.

$('input[type=checkbox]').each(function () {

I now need to make this code more restrictive because i now have other checkboxes on this page. how can i change this so it only loops through all checkboxes with a certain class name?

Upvotes: 0

Views: 510

Answers (5)

epignosisx
epignosisx

Reputation: 6192

No need to include 'input' in the selector:

$(':checkbox.class')

Upvotes: 1

Frenchi In LA
Frenchi In LA

Reputation: 3169

how about :

 $('input:checkbox.class')

Upvotes: 0

SDG
SDG

Reputation: 183

If you're just making a selection, does this really need to be in a loop?

This will return a jQuery object matching your criteria:

$('input.className[type=checkbox]')

if you need it in a loop:

$('input[type=checkbox]').each(function() {
    if ($(this).hasClass('className')) {
         // Do Stuff Here
    }
});

Upvotes: 0

bv8z
bv8z

Reputation: 975

$('input[type=checkbox].myClassName').each(function () {

Upvotes: 0

sdleihssirhc
sdleihssirhc

Reputation: 42496

$('input[type=checkbox].class').each(function () {

Upvotes: 4

Related Questions