nandu.com
nandu.com

Reputation: 343

Jquery to find which checkbox is checked?

I have 10 checkboxes in my page, dynamically created by jquery.

Its starts like chk1 , chk2 and....so on.

I want to get which checkbox is checked out of 10 checkboxes .

for (i=0; i < tbl01.length; i++)
{
if ($("input[type=checkbox][checked]"))
{
var checked = chk+i;

}
}

where

tbl01

is my dataset to dynamically create my checkboxes

I have tried to some extent, is this correct ?

Upvotes: 1

Views: 876

Answers (1)

David Fullerton
David Fullerton

Reputation: 3254

Use the :checked selector:

$("input[type=checkbox]:checked")

Also, if you're just checking whether an element exists, you'll want to check .length on the returned selector (because jquery always returns an object, it just might not have anything in it):

if ($("input[type=checkbox]:checked").length) {
  // do stuff
}

Upvotes: 6

Related Questions