LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Problem with javascript DOM programming (https://www.linuxquestions.org/questions/linux-software-2/problem-with-javascript-dom-programming-412188/)

tooparam 02-06-2006 06:57 AM

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.

Spudley 02-07-2006 06:28 AM

I answered a very similar question here on LQ a few days ago. Here's the link:

http://www.linuxquestions.org/questi....php?p=2080048


All times are GMT -5. The time now is 10:39 PM.