SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Java script to remove html tags TR

    Hi Guys,

    I'm new at JS.
    I need to remove TR elements from parent table but the problem is there are no table ID/Name

    Is it possible to perform it?
    Please see attach - i need remove red marked block... what scrip i have to use if i will put it to the green block?
    Thank you.
    Attached Images Attached Images

  2. #2
    SitePoint Enthusiast TriLLi's Avatar
    Join Date
    Feb 2010
    Location
    BiH, Bugojno
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,

    yes you can remove all tr-s from parent table, for example if you have structure like this

    Code HTML4Strict:
    <table>
      <tr>
          <td><a href="#" onclick="remove_rows(this); return false;">remove all rows</a>
          </td>
       </tr>
      <tr>
          <td><a href="#" onclick="remove_rows(this); return false;">remove all rows</a>
          </td>
       </tr>
      <tr>
          <td><a href="#" onclick="remove_rows(this); return false;">remove all rows</a>
          </td>
       </tr>
      <tr>
          <td><a href="#" onclick="remove_rows(this); return false;">remove all rows</a>
          </td>
       </tr>
      <tr>
          <td><a href="#" onclick="remove_rows(this); return false;">remove all rows</a>
          </td>
       </tr>
      <tr>
          <td><a href="#" onclick="remove_rows(this); return false;">remove all rows</a>
          </td>
       </tr>
      <tr>
          <td><a href="#" onclick="remove_rows(this); return false;">remove all rows</a>
          </td>
       </tr>
    </table>

    then you can remove them like this

    Code JavaScript:
    function remove_rows(el)
    {
     var table = el.parentNode.parentNode.parentNode;
     var rows = table.rows.length;
     for(var i = rows-1; i > = 0; i--)
     {
        table.deleteRow(i);
     }
    }

    I hope that this is what you are looking for
    http://www.wiseblog.info
    Programmers don't die, they just GO SUB without return.

  3. #3
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A simpler version of the same script:
    Code JavaScript:
    function remove_rows(el) {
        var row = el.parentNode.parentNode;
        row.parentNode.removeChild(row);
    }
    (BTW, TriLLi, your table variable is slightly misnamed. It should be called tbody (unless you are using XHTML served as an application of XML).)
    Birnam wood is come to Dunsinane


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
  •