Jump to content

Pass values to column 1 and 2


harshpandya

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...