Skip to content Skip to sidebar Skip to footer

How To Get The List Of Items That Have A Particular Subitem?

I have table where each row starts with a 'checkbox', I'd like to get all the TR that contains a checkbox and it is check. Something like this: $('#' + tableId + ' > tbody >

Solution 1:

Use jQuery's has:

$('#' + tableId + ' > tbody > tr:has(td > input[type=checkbox]:checked)').each(function (i, item) {

  alert('delete ' + $(item).data('row-id'));

});

For better performance (see the jQuery API docs), use this:

$('#' + tableId + ' > tbody > tr')
.filter(':has(td > input:checkbox:checked)').each(function (i, item) {

  alert('delete ' + $(item).data('row-id'));

});

Since the has query cannot be run through querySelectorAll(), there's no need to use [type=checkbox].

Solution 2:

You don't need to :contains selector.

If you want it to be anywhere inside the <tr> (even inside nested elements)

$('#' + tableId + ' > tbody > tr td > input[type=checkbox]:checked').each(function (i, item) {

If you want it directly inside of it

$('#' + tableId + ' > tbody > tr > td > input[type=checkbox]:checked').each(function (i, item) {

Solution 3:

You could just grab the checkboxes and then find their closest tr:

$('#' + tableId + ' > tbody > tr > td > input[type=checkbox]:checked').closest("tr")

Note, you're also missing ) to close the contains.

Post a Comment for "How To Get The List Of Items That Have A Particular Subitem?"