harshpandya Posted June 23, 2008 Share Posted June 23, 2008 I have this code to insert new Row in the Table -- Now i want to pass some values to td1 and td2 columns. How do i pass the values????Please help Thanks Much<script LANGUAGE="JavaScript"> function addRow(id){ var tbody = document.getElementById (id).getElementsByTagName("TBODY")[0]; var row = document.createElement("TR") var td1 = document.createElement("TD") td1.appendChild(document.createTextNode("column 1")) var td2 = document.createElement("TD") td2.appendChild (document.createTextNode("column 2")) row.appendChild(td1); row.appendChild(td2); tbody.appendChild(row); } </script> Add the following HTML code to design the layout of your web page. <BODY bgcolor=#009999><center><h3>Dont forget to visit www.devasp.com<br><a href="http://www.devasp.com">www.devasp.com</a></h3> <a href="java script:addRow('myTable')">Add row</a><table id="myTable" cellspacing="0" border="1"> <tbody> <tr> <td>row1_column1</td><td>row1_column1</td> </tr> </tbody></table></center> </body>Please Help - I want to pass values to td1 and td2.Thanks, Link to comment Share on other sites More sharing options...
justsomeguy Posted June 23, 2008 Share Posted June 23, 2008 You can either use the childNodes property to access all of the children of the TR, or you can give the TD elements an ID and access them by ID. Link to comment Share on other sites More sharing options...
harshpandya Posted June 24, 2008 Author Share Posted June 24, 2008 Can any body give me a simple example or just web link with an example, for both the methods.Thanks, Link to comment Share on other sites More sharing options...
justsomeguy Posted June 24, 2008 Share Posted June 24, 2008 Do you just want to set a value when you create the row? If so, you can add some parameters to the function to do that: function addRow(id, col1, col2){...var td1 = document.createElement("TD")td1.appendChild(document.createTextNode(col1))var td2 = document.createElement("TD")td2.appendChild (document.createTextNode(col2))... You could also add a prompt or do whatever you want to do to get the values in the function: function addRow(id){ var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0]; var row = document.createElement("TR") var td1 = document.createElement("TD") td1.appendChild(document.createTextNode(prompt("Column 1"))) var td2 = document.createElement("TD") td2.appendChild (document.createTextNode(prompt("Column 2"))) row.appendChild(td1); row.appendChild(td2); tbody.appendChild(row);} If that's not what you're trying to do, when are you trying to add the values? Link to comment Share on other sites More sharing options...
harshpandya Posted June 24, 2008 Author Share Posted June 24, 2008 I was trying to do the first one that you suggested. But it was not working but then i realize that when i call addrow(myTable, cell1, cell2); it did not work. It worked when i put addrow('myTable', cell1, cell2);it was string problem. But thanks,You are Cool Man- you help me everytime Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.