473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

adding the rows in a table

I have this piece of code, wherin I would like to add the "amount" col to SubTotal each time a row is added and delete the "amount" from SubTotal each time a row is deleted.
Any help?

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4.  
  5.  
  6.  
  7. function addRow(tableId, cells){
  8.          var tableElem = document.getElementById(tableId);
  9.          var newRow = tableElem.insertRow(tableElem.rows.length);
  10.          var newCell;
  11.         for (var i = 0; i < cells.length; i++) {
  12.                 newCell = newRow.insertCell(newRow.cells.length);
  13.                 newCell.innerHTML = cells[i];
  14.         }
  15.         return newRow;
  16. }
  17.  
  18.  
  19. function deleteRow(tableId, rowNumber){
  20.         var tableElem1 = document.getElementById(tableId);
  21.         if (rowNumber > 0 && rowNumber < tableElem1.rows.length) {
  22.                 tableElem1.deleteRow(rowNumber);
  23.          } else {
  24.           alert("Failed");
  25.          }
  26. }
  27.  
  28. </script>
  29. </head>
  30.  
  31.  
  32. <body>
  33. <table id="tblPeople" border="1">
  34. <tr>
  35.  <th>Hour</th>
  36.  <th>Rate</th>
  37.  <th>Amount </th>
  38. </tr>
  39. </table>
  40. <hr>
  41. <form name="formName">
  42.  Hour: <input type="text" name="Hour"><br>
  43.  Rate: <input type="text" name="Rate"><br>
  44.   <input type="hidden" name="amount" id="amount"><br>
  45.   Subtotal:<input type="text" name="subtotal" id="subtotal"><br>
  46.  <input type="button" value="Add Name"
  47.   onclick="addRow('tblPeople',
  48.    [this.form.Hour.value, this.form.Rate.value, this.form.Hour.value  * this.form.Rate.value ] )" ;>
  49.  <hr>
  50.  Remove Row: <input type="text" size="1" name="RowNum">
  51.  <input type="button" value="Delete Row"
  52.   onclick="deleteRow('tblPeople', this.form.RowNum.value)">
  53. </form>
  54. </body>
  55. </html>
Oct 23 '11 #1
4 1665
Dormilich
8,658 Expert Mod 8TB
you would probably have to re-do the (sub)total calculation each time a row is added or removed.
Oct 23 '11 #2
THANKS Dormilich,
What I am trying to ask is how do I access "amount" from Java Script and fairly print SubTotal(used document.write, but it seems if you print html document.print does not work).
Can I have something as a global variable, I am fairly new to HTML and JS, this is my first try :-)
Oct 23 '11 #3
Dormilich
8,658 Expert Mod 8TB
neither will document.write() work (because of how it works internally) nor is a global variable useful (all the required data are already in the table.

it’s too late now so I will look into it tomorrow.
Oct 23 '11 #4
RamananKalirajan
608 512MB
Hi Sayantan,
You can achieve it. Whenever you are adding a row or deleting a row make a call to another function. From the first row you can add up the third column value and sum it and display it on the subtotal. The following is a sample I write here (Not checked)

Expand|Select|Wrap|Line Numbers
  1. function doSubTotal(){
  2.    var tblObj = document.getElementById('tblPeople');
  3.    var rowLen = tblObj.rows.length;
  4.    var subTotal = 0;
  5.    for(var i=1;i<tblObj.rows.length;i++){
  6.       var rowObj = tblObj.rows[i];
  7.       subTotal+=parseInt(tblObj.rows[i].cells[rowObj.cells.length - 1].childNodes[0].data);
  8.    }
  9.    document.getElementById('subtotal').value=subTotal;
  10. }
Please let me know if you have any doubts

Thanks and Regards
Ramanan Kalirajan
Oct 24 '11 #5

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

Similar topics

5
by: Sue | last post by:
On code-behind page: (attributes set programatically for each of these elements) linkbutton added to tablecell textbox added to tablecell tablecells added to tablerow tablerow added to table...
2
by: Clayton Hamilton | last post by:
I have a DataGrid on a webform bound to a Datasource and can successfully use <ItemTemplate> to create edit/update/cancel functionality for user maintenance of data. I use separate logic to delete...
0
by: Fabrizio | last post by:
Hi, I need to dynamically add rows and cells with controls like textbox to an empty table. After adding controls to cells, i add cell to rows and at the end add the rows to the table, but the...
3
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
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
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...
0
by: Luis Esteban Valencia | last post by:
Hello I wrote a program with code behind in C# to add row into table dynamically and the program worked very well in .Net Framework 1.1. When I run this program in .Net Framework 2.0 beta...
4
by: VMI | last post by:
I'm working on a questionaire, and I'd like to add the questions to a gridview, so that each row (of two "columns" each) has the question in one field, and the editable section (where the user will...
0
by: ganesh22 | last post by:
Hi... Iam using GridView in asp.net(2.0) .My requirement is user can add,update,delete in gridview I written the code for add & update but can u help adding rows in grid view My code: ...
0
by: DotNetFAQ | last post by:
I need to insert rows to a database table after processing a piece of an XML file. The XML file would look something like: <Book> <Chapter> <Page> <Paragraph> <Paragraph> </Page> </Chapter>...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.