473,503 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding multiple rows..

2 New Member
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..:(
Oct 14 '09 #1
3 4912
acoder
16,027 Recognized Expert Moderator MVP
By multiple, do you mean a fixed number, e.g. 2, or a number that you want to allow the user to decide?
Oct 14 '09 #2
didi86
2 New Member
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..:)
Oct 15 '09 #3
acoder
16,027 Recognized Expert Moderator MVP
Just extend the code in addRowToTable(). Which part of that code do you not understand?
Oct 16 '09 #4

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

Similar topics

4
5449
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
3
4855
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
1
3260
by: Andrew | last post by:
Hey all, I am very new to ASP.Net (and .Net in general), but that isn't stopping the boss from wanting to begin new projects in it. This latest project has me kinda stumped and after a couple...
0
1863
by: Sileesh | last post by:
Hi I have html table and a Button in an Aspx page. I am adding one row with some textboxes to Html table each time i click on the Button thru Javascript. Now problem is when when i try to...
2
2421
by: Bob Hollness | last post by:
Hi group. I am a newbie to ASP.NET as you will see from some of the questions I may ask! I have a datagrid which I have populated from a database. It works great! I have added a column, via...
2
1990
by: Amy | last post by:
Is it possible to do so?
0
1825
by: EricLondaits | last post by:
Hi, I have an ASP.NET page with a ListBox that is data bound to a table with a single field (it holds a list of valid IDs). The page also has a textBox into which you can add new valid IDs, one...
2
4601
by: mdfayazi | last post by:
I am using dataadapter to Add,Update,Delete rows in a datagridview.Update and delete are working but while adding a rows the problem comes.When I add more than one row only first row values are...
4
4452
by: Lewis Holmes | last post by:
Hi I have the following situation in one of my asp.net pages. The user can add multiple table rows to a form by selecting a button. These rows can contain asp.net controls. When this button is...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7089
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.