.each
always returns the jQuery object containing all elements that you iterated over so:
function getSingleCheckedItemId() {
var ret;
$(".data-table-chk-item").each(function() {
if ($(this).is(":checked")) {
ret = $(this).attr("value");
return false; //breaks out of .each
}
});
return ret;
}
Also, this.value
is usually a better option than $(this).attr('value')
in case you're dealing with form inputs - seems like you have radio
/checkbox
inputs due to their checked
property. Also, this.checked
returns a boolean so there's no need for $(this).is(':checked')
either.
I believe your logic can be simplified to:
function getSingleCheckedItemId() {
return $(".data-table-chk-item:checked").val();
}
This way .val()
will return the value of the first :checked
item or undefined
if no elements are matched by the selector, which does the same as the loop above.