I am trying to get all <TR> tags within <TABLE> tag in an array and I am trying to refer it like arrayname[1], arrayname[2] . Then I am looking forward to set a unique title attribute to <TR> tag’s, using idArr[1].setAttribute("title", "myClass");
Here is the code I have written, which does not works. Can anyone guide me what’s wrong in it !!
<html>
<head>
<script type="text/javascript" >
var idArr = [];
var formm=document.getElementById("form1");
var trs = formm.getElementsByTagName("tr");
alert(trs.length);
for(var i=0;i<trs.length;i++)
{
idArr.push(trs[i].id);
alert(idArr.innerText);
}
idArr[1].setAttribute("title", "myClass");
</script>
</head>
<body>
<form id="form1">
<table border="1">
<tr>
<td>One</td>
<td>Two</td>
</tr>
<tr>
<td>ABC</td>
<td>EFG</td>
</tr>
</table>
</form>
</body>
</html>
A couple considerations:
So I got rid of the extra Array code, and I moved the script to the bottom of the page so that it doesn’t run until the DOM above it has loaded.