By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

Dynamically Adding row and all value storre in variable

P: 57
i add row dynamiccaly but value doesn't pprint. please help me.

My coding is as follow

Expand|Select|Wrap|Line Numbers
  1. <HTML>  
  2. <HEAD>  
  3.      <TITLE> Add/Remove dynamic rows in HTML table </TITLE>  
  4.      <SCRIPT language="javascript">  
  5.          function addRow(tableID) {  
  6.              var table = document.getElementById(tableID);  
  7.              var rowCount = table.rows.length;  
  8.              var row = table.insertRow(rowCount);  
  9.              var cell1 = row.insertCell(0);  
  10.              var element1 = document.createElement("input");  
  11.              element1.type = "checkbox";  
  12.              cell1.appendChild(element1);  
  13.  
  14.              var cell2 = row.insertCell(1);  
  15.              cell2.innerHTML = rowCount + 1;  
  16.  
  17.              var cell3 = row.insertCell(2);  
  18.              var element2 = document.createElement("input");  
  19.              element2.type = "text";  
  20.  
  21.              //element2.value =document.getElementById().value;
  22.              cell3.appendChild(element2);  
  23.         }  
  24.  
  25.          function deleteRow(tableID) {  
  26.              try {  
  27.              var table = document.getElementById(tableID);  
  28.              var rowCount = table.rows.length;  
  29.  
  30.              for(var i=0; i<rowCount; i++) {  
  31.                  var row = table.rows[i];  
  32.                  var chkbox = row.cells[0].childNodes[0];  
  33.  
  34.                  if(null != chkbox && true == chkbox.checked) {  
  35.                      table.deleteRow(i);  
  36.                      rowCount--;  
  37.                      i--;  
  38.                  }  
  39.              }  
  40.              }catch(e) {  
  41.                  alert(e);  
  42.              }  
  43.          }  
  44.      </SCRIPT>  
  45.  </HEAD>  
  46.  
  47.  <BODY>  
  48.      <INPUT type="button" value="Add Row" onClick="addRow('dataTable')" />  
  49.      <INPUT type="button" value="Delete Row" onClick="deleteRow('dataTable')" />  
  50.  
  51.      <TABLE id="dataTable" width="350px" border="1">  
  52.          <TR>  
  53.              <TD><INPUT type="checkbox" name="chk"/></TD>  
  54.              <TD> 1 </TD>  
  55.              <TD> <INPUT type="text" /> </TD>  
  56.          </TR>  
  57.      </TABLE>  
  58.  </BODY>  
  59.  </HTML> 
Mar 3 '10 #1
Share this Question
Share on Google+
1 Reply


100+
P: 210
What do you mean by "value doesn't print"?

If you want to copy the value of the previous field, where you have:

Expand|Select|Wrap|Line Numbers
  1. //element2.value =document.getElementById().value;
  2.  
Insert:
Expand|Select|Wrap|Line Numbers
  1. if( rowCount > 0 )  
  2.  element2.value = table.rows[ rowCount-1 ].cells[ 2 ].getElementsByTagName('input')[0].value;
  3.  
  4.  
Be careful about using childnodes, since some browsers include newline characters in the markup.
Mar 4 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.