I have an HTML page, which has a table in it, I have dynamically added a third row of checkbox in it. When I access the cells of rows inside onclick method of checkbox it shows me error.
my javascript code goes like this:
tbl = document.getElementsByTagName("body")[0];
tr = tbl.getElementsByTagName("tr");
for (i = 1; i < tr.length; i++) {
var td = document.createElement('td');
var input = document.createElement('INPUT');
input.type = 'checkbox';
input.onclick = function () {
for(var j=0;j<tr[i].cells.length;j++) //error occur on this line
//Uncaught TypeError: Cannot read property 'cells' of undefined
at HTMLInputElement.input.onclick
{
console.log(tr[i].cells[j].innerHTML);
}
};
td.appendChild(input);
tr[i].appendChild(td);
}
My corresponding HTML code goes like this:-
<!DOCTYPE html>
<html>
<head>
<style>
table, td {
border: 1px solid black;
}
</style>
<title>PHP MyAdmin</title>
</head>
<body>
<table border="1">
<tr>
<th>Email_id</th>
<th>Email_content</th>
</tr>
<tr>
<td>akshay1234530@yahoo.com</td>
<td>bla bla</td>
</tr>
<tr>
<td>ohm3966@gmail.com</td>
<td>bla bla</td>
</tr>
</table>
</body>
</html>
Please help. Thanks in advance.