Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through the list and get checked checkbox id in jquery

I have got id's of all the row which have check box in a variable.

var allIds = jQuery("#progAccessSearchResults").jqGrid("getDataIDs");

Now I have to iterate over this and get id's of only checked check boxes. I tried following code to get checked checkbox id.

var boxes = $(":checkbox:checked");

But it is not working. Help me out..!! I am new to javascript n jquery. So pls don't mind if it is a silly problem..!!

like image 855
Sanjay Malhotra Avatar asked Nov 30 '22 12:11

Sanjay Malhotra


1 Answers

You can use .map to find all IDs of checked checkboxes, using your existing selector:

HTML:

<input type="checkbox" id="cb1" />
<input type="checkbox" id="cb2" />
<input type="checkbox" id="cb3" checked="checked" />
<input type="checkbox" id="cb4" />
<input type="checkbox" id="cb5" checked="checked" />

Javascript:

var checkedIds = $(":checkbox:checked").map(function() {
        return this.id;
    }).get();

Returns: [ "cb3", "cb5" ]

Here is a fiddle for the above code.

like image 165
CodingIntrigue Avatar answered Dec 10 '22 17:12

CodingIntrigue