What I'm trying to do here is validating HTML table. I need at least one checkbox to be checked before proceeding. It's doing what it's need to do, except when I don't check a checkbox.
It loops the alert "nothing found" by the count of data on the HTML table. How can I make it better? I need to loop inside the table to get the data on the same row with the checkbox checked.
JS
$('#dataTable').find('tr').each(function () {
var row = $(this);
if (row.find('input[type="checkbox"]').is(':checked') ) {
alert "found"
//im getting data here.
});
}else{
alert "NOthing found"
};
});
change(function () { if ($("input[type='checkbox']:checked"). length > 0) $("#b1"). prop("disabled", false); else $("#b1"). prop("disabled", true); });
The checked attribute is a boolean attribute. When present, it specifies that an <input> element should be pre-selected (checked) when the page loads. The checked attribute can be used with <input type="checkbox"> and <input type="radio"> . The checked attribute can also be set after the page load, with a JavaScript.
No need to loop through anything. Just look for a checked checkbox inside dataTable with your selector. If it exists, then your "true" condition is met.
if ($('#dataTable input[type="checkbox"]:checked').size()) {
// Proceed
}
Now, if you want to do something with the data inside the "checked" rows, you could do something like this:
var checkedItems = $('#dataTable input[type="checkbox"]:checked').each(function() {
// Do something with the row
console.log($(this).parent('tr').find('.data-selector').val());
});
if (!checkedItems.size()) {
// Nothing was checked
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With