SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Uruguay
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How create tr in the middle of a table dinamically

    Hi
    I have a table with several tr (and td) inside, each tr has an ID
    I would like to know how can I create an extra tr in the middle of the table.
    I know how to create dom elements but I don't know how to append it not at the end, but between tr 5 and tr 6 for example.

    Thanks.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tBody.insertBefore(newTR, tr6);

    instead of

    tBody.appendChild(newTR);

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Uruguay
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Uruguay
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is not working with tables, do you know why?

    HTML Code:
    <table id="list" class="list">
        <tbody>
        <tr>
        </tr>
        <tr id="28">
         <td><a href="#" onclick="myF(28)">dada</a></td>
        </tr>
         <tr id="29">
         <td><a href="#" onclick="myF(29)">dada</a></td>
        </tr>
        <tr id="15">
         <td><a href="#" onclick="myF(15)">dada</a></td>
        </tr>
        </tbody>
        </table>
    Code:
    <script>
    function myF(id){
        var newTr=document.createElement('tr');
        var newTrTd=document.createElement('td');
        newTrTd.appendChild(document.createTextNode('Cancel'));
        newTr.appendChild(newTrTd);
        document.getElementById("list").insertBefore(newTr, document.getElementById(id))
      }
    
    </script>

    When I click on 29, FF return: Node was not found" code: "8

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to append the new row to the tbody, not the table

    document.getElementById("list").getElementsByTagName("tbody")[0].insertBefore(newTr, document.getElementById(id))

  6. #6
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Uruguay
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ouuh, you are right, now it works, thank you!

  7. #7
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The value of the id attribute may not start with a number.


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
  •