function getRowIndex() {
var tble = document.getElementById("myTable");
for (var i = 0; i < tble.rows.length; i++) {
for (var j = 0; j < tble.rows[i].cells.length; j++) {
tble.rows[i].cells[j].onclick = function() {
return this.parentElement.rowIndex;
}
}
}
}
var rIndex = getRowIndex();
console.log(rIndex);
This function getRowIndex() is returning undefined when I try to print index. I want to extract the row index value in value when clicked, using javaScript.
getRowIndexyou have created an anonynous callback function with a return statatement for onclick property of cells, but nothing that would return stuff from getRowIndexgetRowIndexseems to be a function that assigns click event handlers to the cells of your table, so its name is misleading. Please explain what you are trying to do exactly, because that function does not seem like it should be returning any index.