By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,930 Members | 1,383 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,930 IT Pros & Developers. It's quick & easy.

append a HTML string to a Table

mickey0
100+
P: 142
hi my code is this:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function myFunc () {
  3.     var s = "<tr><td><a href=\"http://www.google.com\">Hello</a></td></tr>";
  4.     var elem = document.getElementById("intro").insertRow(-1);  
  5.     //document.writeln(s);    
  6.     var linkText = document.createTextNode(s); //this cannot be a Text
  7.     elem.appendChild(linkText);
  8. }   
  9. </script>
  10.  <body onload="myFunc()">
  11.     <table  border="5" >       
  12.        <tbody id="intro">  </tbody>
  13.     </table>
  14. </body>
  15.  
I need to render the string s between tbody. Is there a solution, please?

Thanks.
Jan 22 '08 #1
Share this Question
Share on Google+
3 Replies


Delerna
Expert 100+
P: 1,134
will something like this do

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.    <head>
  3.       <script language="javascript">
  4.          var cnt=2;
  5.  
  6.          function AddRow()
  7.          {  
  8.             var table = document.getElementById("myTable");
  9.             if (!table) throw "Table not found";
  10.  
  11.             var row = table.insertRow(-1); //append at the end
  12.  
  13.             var cell1 = row.insertCell(-1);
  14.             var cell2 = row.insertCell(-1);
  15.             var cell3 = row.insertCell(-1);
  16.  
  17.             cell1.setAttribute('align','right');
  18.  
  19.             cell1.innerHTML = cnt;
  20.             cell2.innerHTML = 'test' + cnt.toString();;
  21.             cell3.innerHTML = 'test';
  22.  
  23.  
  24.             cnt++;
  25.          }
  26.  
  27.      function RemoveRow()
  28.      {  var table = document.getElementById("myTable");
  29.         table.deleteRow(-1);
  30.      }
  31.       </script>
  32.    </head>
  33.  
  34.    <body>
  35.       <table><tr>
  36.       <td><input type="button" onClick="AddRow();" value="Add Row"></td>
  37.       <td><input type="button" onClick="RemoveRow();" value="Remove Row"></td>
  38.       </tr></table>
  39.  
  40.       <table rules='none' cellspacing=0 frame='box' id="myTable">
  41.          <tr>
  42.             <td>1</td>
  43.             <td>sam</td>
  44.             <td>stephens</td>
  45.          </tr>
  46.       </table>
  47.  
  48.  
  49.  
  50. </body>
  51. </html>
  52.  
Jan 22 '08 #2

mickey0
100+
P: 142
hello,
my problem isn't how insert a row in the table; my problem is append (i Say append because I found that innerHTML doens't work properly with IE) an element like
var s = "<tr><td><a href=\"http://www.google.com\">Hello</a></td></tr>";
If I create and ElementText, s will append as string; but I don't want it....and the id="intro" is on tbody....
Jan 22 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Why not use the DOM methods instead? You might find this link helpful.
Jan 23 '08 #4

Post your reply

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