Go through the list and check check check id in jquery

I have an identifier for the entire row that has a checkbox in a variable.

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

Now I need to iterate over this value and check only checked flags. I tried the following code to set the checkbox id.

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

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

+4
source share
4 answers

You can use .map to find all identifiers of checked flags using an 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" ]

.

+15

each jquery

$("input:checkbox").each(function(){
    var $this = $(this);    
    if($this.is(":checked")){
        console.log($this.attr("id"));
    }
});
+4

var selected = new Array();
$('input:checked').each(function() {
    selected.push($(this).attr('id'));
});

selected. jsfiddle

+1

: nth-child , <td>, . >input:checked, . , jQuery, , .closest("tr.jqgrow") , . - , . $.map .

$("#getIds").button().click(function () {
    var $checked = $grid.find(">tbody>tr.jqgrow>td:nth-child(" +
                   (iCol + 1) + ")>input:checked"),
        ids = $.map($checked.closest("tr.jqgrow"),
                    function (item) { return item.id; });
    alert("The list of rowids of the rows with checked chechboxs:\n" + ids.join());
});

iCol getColumnIndexByName

var getColumnIndexByName = function (grid, columnName) {
    var cm = grid.jqGrid("getGridParam", 'colModel'), i, l;
    for (i = 0, l = cm.length; i < l; i += 1) {
        if (cm[i].name === columnName) {
            return i; // return the index
        }
    }
    return -1;
};

.

The demo demonstrates the above code in real time.

+1
source

All Articles