Techonthenet
Techonthenet

Reputation: 157

Get Checked Checkbox values

I have table with check boxes in it.and the CSS for the check box is GridCheckBox, I want to read the check boxes tool tip into an array with , separated.

I am using this function. But this not getting the checked items. but getting the unchecked items correctly.

This works well

var titles = [];
$('#ListGrid .GridCheckBox').each(function() {
    if ($(this).attr('title')) {
        titles.push($(this).attr('title'));
    }
});

And this not working(Checked items)

var titles = [];
$('#ListGrid .GridCheckBox:checked').each(function() {
    if ($(this).attr('title')) {
        titles.push($(this).attr('title'));
    }
});

Upvotes: 0

Views: 2545

Answers (3)

Limpan
Limpan

Reputation: 682

Well another way to do it is to use jQuery $.map() in this example i grab all selectboxes by name. For example:

var titles = $.map($("input[name='name_of_checkboxes']:checked"),function(a){return a.title;});

Shuld return a array with selected - selectbox titles.

EDIT

To get none selected selectboxes you culd use this

  var not_selected = $.map($("input[name='name_of_checkboxes']:not(:checked)"),function(a){return a.title;});

HTML:

<input type="checkbox" name="name_of_checkboxes" checked="checked" title="1" />
<input type="checkbox" name="name_of_checkboxes" title="2" />
<input type="checkbox" name="name_of_checkboxes" checked="checked" title="3" />
<input type="checkbox" name="name_of_checkboxes" title="4"/>

Over n out

Upvotes: 1

Yman
Yman

Reputation: 934

Of cos it doesn't work. The class ".GridCheckBox" u applied on is on the parent "span" and not the "checkbox input" itself!

you can do this though:

$('#ListGrid .GridCheckBox').each(function() {
var target = $(this);
if (target.find("input:checkbox").is(":checked")) {
    titles.push(target.attr('title'));
}});

assuming one span.GridCheckBox contains only one checkbox input.

Upvotes: 0

Alnitak
Alnitak

Reputation: 339816

To try to eliminate any mistakes, and also to aid performance, try this instead:

$('#ListGrid .GridCheckBox').filter(':checked').each( ..

Because :checked is a jQuery extension it doesn't perform as well as the native browser methods for matching objects.

Upvotes: 0

Related Questions