Problem with javascript DOM programming
Hi there
I want to insert a row in a table using Javascript DOM programming. But I get the error mentioned below when I run the below script in Mozilla. Though I am able to appenChild successfully, I am not able to insert the row. If you have any such experience, please help.
<html>
<head>
<script language="javascript1.2">
function insert()
{
newTD=document.createElement("td");
newText=document.createTextNode("inserted node");
newTD.appendChild(newText);
newTR=document.createElement("tr");
newTR.appendChild(newTD);
mytable=document.getElementById("mytable");
oldTR=mytable.getElementsByTagName("tr").item(1);
mytable.insertBefore(newTR,oldTR);
}
</script>
</head>
<body>
<table border="1" id="mytable">
<tr>
<td>Td one row one</td>
<td>td two row one</td>
</tr>
<tr>
<td>Td one row two</td>
<td>td two row two</td>
</tr>
</table>
<input type=button value="Insert Before" onclick = 'insert()'>
</body>
</html>
Error: uncaught exception: [Exception... "Node was not found" code: "8" nsresult: "0x80530008 (NS_ERROR_DOM_NOT_FOUND_ERR)" location: "file:///root/LearnDOM/insert2.htm Line: 13"]
In my case line no . 13 is mytable.insertBefore(newTR,oldTR);
Please help me and show me the right direction.
|