SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict Scott Blanchard's Avatar
    Join Date
    Sep 2010
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Remove table rows from a table via script by index?

    Can I use script to remove the 2nd and 3rd table row elements from this html?

    Code:
     <table id="dap_product_links_table"> 
            <tr> 
              <td><span class="scriptheader">Product Title</span></td> 
            </tr> 
            <tr> 
              <td><strong>Access Start Date</strong>: 2010-06-28</td> 
            </tr> 
            <tr> 
              <td><strong>Access End Date</strong>: 2011-06-28</td> 
            </tr>
    If so, how?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Scott Blanchard View Post
    Can I use script to remove the 2nd and 3rd table row elements from this html?
    There are several different ways, but they depend on how you may be later on intending to use the content.

    Do you want every table row except for the first one to be removed?
    Do you want only the 2nd and 3rd ones being removed?
    Do you want only the last two being removed?

    Here's one potential example of the first technique:

    Code javascript:
    var table = document.getElementById('dap_product_links_table'),
        trs = table.getElementsByTagName('tr');
    while (trs.length > 1) {
        trs[trs.length - 1].parentNode.removeChild(trs[trs.length - 1]);
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict Scott Blanchard's Avatar
    Join Date
    Sep 2010
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Paul, thanks for your answer. I just need rows 2 and 3, regardless of how many rows. I can take your code and do the do.

    Code:
    while (trs.length > 1 && trs.length < 3 ) {
    Correct?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Scott Blanchard View Post
    Hi Paul, thanks for your answer. I just need rows 2 and 3, regardless of how many rows. I can take your code and do the do.

    Code:
    while (trs.length > 1 && trs.length < 3 ) {
    Correct?
    Right.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •