SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to dynamically add rows to a table?

    Hi people

    I have the following table definition:
    <table id="mytable" cellpadding="0" cellspacing="0"></table/>
    I need to add rows into that table in JavaScript.
    First I tried something like this:

    var table = document.getElementById('mytable');
    table.innerHTML += newRow;

    Where newRow is a variable that holds the <tr><td>...</td></tr> content.

    This solution didn't work in IE and after reading, I found out that the innerHTML attribute is read only for the <table> tag in IE. In Firefox it works but this is not good enough for me...

    My question: What would be the recommended way of dynamically adding rows to a table in JavaScript. I am looking for a solution that will work across browsers of course

    regards
    Jason

  2. #2
    Motivated Procrastinator seriocomic's Avatar
    Join Date
    Jan 2003
    Location
    Outside the bubble
    Posts
    1,275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm no expert, but achieved a similar thing using JQuery (using append or prepend)

    The downside is having to add the library rather than just the javascript required to make the addition. The upside is that JQuery has a good work rate across all browsers.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •