Solved

Dynamically add row to table

Posted on 2001-08-05
9
283 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
Comment Utility
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
Comment Utility
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
Comment Utility
which one ?
0
 

Author Comment

by:kingasa
Comment Utility
COBOLdinosaur,

run your example with no luck why?
(IE 5.5)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 53

Expert Comment

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

Cd&
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
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
Comment Utility
<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
Comment Utility
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
Comment Utility
Glad I could help. Thanks for the A.:^)

Cd&
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

772 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

10 Experts available now in Live!

Get 1:1 Help Now