I am trying to validate a text input when it loses focus. I would like to know which row of the table it is in. This is what I have so far and it keeps coming back as undefined. Any ideas?
$("div#step-2 fieldset table tbody tr td input").blur(function() {
var tableRow = $(this).parent().parent();
if ($.trim($(this).val()) == "") {
$(this).addClass("invalid");
alert(tableRow.rowIndex);
$(this).val("");
} else {
$(this).removeClass("invalid");
checkTextChanges();
}
});
rowIndexis a DOM property, not a jQuery method, so you have to call it on the underlying DOM object:or just:
since you aren’t really using jQuery for much there.
In jQuery 1.4 there is
$(row).index(), but it scans siblings to find out which child element number it is in its parent. This is slower and will return a different result torowIndexin the case where you have multiple<tbody>s.