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 > tr:contains(td > input[type=checkbox]:checked').each(function (i, item) {
alert('delete ' + $(item).data('row-id'));
});
That code doesn't work, it gets stuck. Probably because the "contains" selector is only for text. How could I do it?
Regards.
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]
.
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) {
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
.