Connecting Tech Pros Worldwide Help | Site Map

Adding multiple rows..

Newbie
 
Join Date: Oct 2009
Posts: 2
#1: Oct 14 '09
Please help me to adding multiple row at a time...

Expand|Select|Wrap|Line Numbers
  1. // Last updated 2006-02-21
  2.  
  3. <script language="javascript">
  4. function addRowToTable()
  5. {
  6.   var tbl = document.getElementById('tblSample');
  7.   var lastRow = tbl.rows.length;
  8.   // if there's no header row in the table, then iteration = lastRow + 1
  9.   var iteration = lastRow;
  10.   var row = tbl.insertRow(lastRow);
  11.  
  12.   // left cell
  13.   var cellLeft = row.insertCell(0);
  14.   var textNode = document.createTextNode(iteration);
  15.   cellLeft.appendChild(textNode);
  16.  
  17.   // right cell
  18.   var cellRight = row.insertCell(1);
  19.   var el = document.createElement('input');
  20.   el.type = 'text';
  21.   el.name = 'txtRow' + iteration;
  22.   el.id = 'txtRow' + iteration;
  23.   el.size = 40;
  24.  
  25.   el.onkeypress = keyPressTest;
  26.   cellRight.appendChild(el);
  27.  
  28.   // select cell
  29.   var cellRightSel = row.insertCell(2);
  30.   var sel = document.createElement('select');
  31.   sel.name = 'selRow' + iteration;
  32.   sel.options[0] = new Option('text zero', 'value0');
  33.   sel.options[1] = new Option('text one', 'value1');
  34.   cellRightSel.appendChild(sel);
  35. }
  36.  
  37.  
  38. function keyPressTest(e, obj)
  39. {
  40.   var validateChkb = document.getElementById('chkValidateOnKeyPress');
  41.   if (validateChkb.checked) {
  42.     var displayObj = document.getElementById('spanOutput');
  43.     var key;
  44.     if(window.event) {
  45.       key = window.event.keyCode; 
  46.     }
  47.     else if(e.which) {
  48.       key = e.which;
  49.     }
  50.     var objId;
  51.     if (obj != null) {
  52.       objId = obj.id;
  53.     } else {
  54.       objId = this.id;
  55.     }
  56.     displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);
  57.   }
  58. }
  59.  
  60.  
  61.  
  62. function removeRowFromTable()
  63. {
  64.   var tbl = document.getElementById('tblSample');
  65.   var lastRow = tbl.rows.length;
  66.   if (lastRow > 2) tbl.deleteRow(lastRow - 1);
  67. }
  68.  
  69.  
  70.  
  71. function openInNewWindow(frm)
  72. {
  73.   // open a blank window
  74.   var aWindow = window.open('', 'TableAddRowNewWindow',
  75.    'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
  76.  
  77.   // set the target to the blank window
  78.   frm.target = 'TableAddRowNewWindow';
  79.  
  80.   // submit
  81.   frm.submit();
  82. }
  83.  
  84.  
  85.  
  86. function validateRow(frm)
  87. {
  88.   var chkb = document.getElementById('chkValidate');
  89.   if (chkb.checked) {
  90.     var tbl = document.getElementById('tblSample');
  91.     var lastRow = tbl.rows.length - 1;
  92.     var i;
  93.     for (i=1; i<=lastRow; i++) {
  94.       var aRow = document.getElementById('txtRow' + i);
  95.       if (aRow.value.length <= 0) {
  96.         alert('Row ' + i + ' is empty');
  97.         return;
  98.       }
  99.     }
  100.   }
  101.   openInNewWindow(frm);
  102. }
  103. </script>
  104.  
  105. <form action="tableaddrow_nw.html" method="get">
  106. <p>
  107. <input type="button" value="Add" onclick="addRowToTable();" />
  108. <input type="button" value="Remove" onclick="removeRowFromTable();" />
  109. <input type="button" value="Submit" onclick="validateRow(this.form);" />
  110. <input type="checkbox" id="chkValidate" /> Validate Submit
  111. </p>
  112. <p>
  113. <input type="checkbox" id="chkValidateOnKeyPress" checked="checked" /> Display OnKeyPress
  114. <span id="spanOutput" style="border: 1px solid #000; padding: 3px;"> </span>
  115. </p>
  116. <table border="1" id="tblSample">
  117.     <table border="1" id="referfs">
  118.     <tr>
  119.   <tr>
  120.   <td> 1.</td>
  121.     <td size = "30">Salutation</td>
  122.     <td>:</td>
  123.         <td>
  124.     <select name="selRow0">
  125.     <option value="value0">Mr</option>
  126.     <option value="value1">Ms</option>
  127.     <option value="value1">Madam</option>
  128.     </select>
  129.     </td>
  130.   </tr>
  131.  
  132.   <tr>
  133.      <td> </td>
  134.     <td size = "30">Friend's Name</td>
  135.     <td>:</td>
  136.     <td><input type="text" name="txtRow1" id="txtRow1" size="20" onkeypress="keyPressTest(event, this);" /></td>
  137.   </tr>
  138.  
  139.     <tr>
  140.      <td> </td>
  141.     <td size = "30">Friend's Email</td>
  142.     <td>:</td>
  143.     <td><input type="text" name="txtRow1" id="txtRow1" size="20" onkeypress="keyPressTest(event, this);" /></td>
  144.   </tr>
  145.  
  146.     <tr>
  147.      <td> </td>
  148.     <td size = "30">Friend's Phone Number</td>
  149.     <td>:</td>
  150.     <td><input type="text" name="txtRow1" id="txtRow1" size="20" onkeypress="keyPressTest(event, this);" /></td>
  151.   </tr>
  152.   </tr>
  153.   </table>
  154. </table>
  155. </form>

Hope anybody can help me to finish this code..:(
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Oct 14 '09

re: Adding multiple rows..


By multiple, do you mean a fixed number, e.g. 2, or a number that you want to allow the user to decide?
Newbie
 
Join Date: Oct 2009
Posts: 2
#3: Oct 15 '09

re: Adding multiple rows..


if u try run tis code and u click add button..only the last row will added..
In my case, I want row start from "Salutation" till row phone number will repeat..means that when click add button, 4 row will added..
thanks..:)
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Oct 16 '09

re: Adding multiple rows..


Just extend the code in addRowToTable(). Which part of that code do you not understand?
Reply