Solved

Dynamically add row to table

Posted on 2001-08-05
9
284 Views
Last Modified: 2010-05-18
How do I dynamically add a row to an existing table
on a button click event.
without reposting the page.
0
Comment
Question by:kingasa
  • 6
  • 3
9 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6353784
IE and maybe NS 6.

Assuming a table with an id of "myTable"

<script language="JavaScript".
<!--
   var DOM1= (document.getElementById) ? true:false;
   var IE4= (document.all && !document.getElementById) ? true:false;
   var ELpntr=false;


   function addRow(content,morecontent)
   {
      if (DOM1) ELpntr=document.getElementById('mytable');
      if (IE) ELpntr =document.all['mytable'];
      // define the HTML string for the new row
      newstr='<tr><td>'+content+'</td><td>'+morecontent+'</td></tr>';
      // get current HTML
      str=ELpntr.innerHTML;
      // update table definition
      str+=newstr;
   }
//-->
</script>

Put that in the head call it when you want to add a row.

If you want to do it in Netscrap 4.x
You will have to put the table in a layer and re-write the whole layer.
To render the new row the browser will have to re-render and will
probably hang or crash.

Cd&

0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6353786
BTW, you have a lot of old open questions which makes me reluctant to spend any more time on this.

Cd&
0
 

Author Comment

by:kingasa
ID: 6353846
which one ?
0
 

Author Comment

by:kingasa
ID: 6354016
COBOLdinosaur,

run your example with no luck why?
(IE 5.5)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6354152
Post the code where you are calling it, probably something simple... I hope.

Cd&
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6354181
Ooops,

I just saw the problem. The last line of the script is missing.

Add one more line to the script:

ELpntr.innerHTML=str;

sorry about that, I forgot to actually rewrite the table.

Cd&
0
 

Author Comment

by:kingasa
ID: 6354787
<html>
<head>
<script language="JavaScript">
  var DOM1= (document.getElementById) ? true:false;
  var IE= (document.all && !document.getElementById) ? true:false;
  var ELpntr=false;
  function addRow(content,morecontent)
  {
     if (DOM1) ELpntr=document.getElementById('mytable');
     if (IE) ELpntr =document.all['mytable'];
     newstr='<tr><td>'+content+'</td><td>'+morecontent+'</td></tr>';
     str=ELpntr.innerHTML;
     str+=newstr;
     ELpntr.innerHTML=str;
  }
</script>

</head>
<body>
<table border='1' id='mytable'>
<tr><td>22</td><td>333</td></tr>
<tr><td>22</td><td>333</td></tr>
</table>

<input type='button' value='AddRow' onClick='addRow("123","456")'>
</body>
</html>
0
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 50 total points
ID: 6356219
Wonderful!  The problem is IE5.5 insists on using the complex table
if you want to dynamically modify the table.  So we have to add a
tbody tag and use the DOM! methods:


<html>
<head>
<script language="JavaScript">
var DOM1 = (document.getElementById) ? true:false;
var IE= (document.all && !document.getElementById) ? true:false;
var ELpntr=false;
function addRow(content,morecontent)
{
   if (IE)
   {
      ELpntr =document.all['mytable'];
      newstr='<tr><td>'+content+'</td><td>'+morecontent+'</td></tr>';
      str=ELpntr.innerHTML;
      str+=newstr;
      ELpntr.innerHTML=str;
   }
   else
   {
      if (DOM1)
      {
         tabBody=document.getElementsByTagName("TBODY").item(0);
         row=document.createElement("TR");
         cell1 = document.createElement("TD");
         cell2 = document.createElement("TD");
         textnode1=document.createTextNode(content);
         textnode2=document.createTextNode(morecontent);
         cell1.appendChild(textnode1);
         cell2.appendChild(textnode2);
         row.appendChild(cell1);
         row.appendChild(cell2);
         tabBody.appendChild(row);
       }
   }
}
</script>

</head>
<body>
<table border='1' id='mytable'>
<tbody>
<tr><td>22</td><td>333</td></tr>
<tr><td>22</td><td>333</td></tr>
</tbody>
</table>
<input type='button' value='AddRow' onClick='addRow("123","456")'>
</body>
</html>

Works great, but why browser manufactures make things that used to be
simple more difficult, I will never know.

Cd&
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6359240
Glad I could help. Thanks for the A.:^)

Cd&
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can I do a Select All on this page? 8 36
Angular 2 complaining about map file 7 53
Form Submit falis 6 25
Alignment is not working correctly. 8 32
Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now