Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. deleteRow() weird behavior, deletes two rows.

deleteRow() weird behavior, deletes two rows.

Scheduled Pinned Locked Moved JavaScript
javascriptquestionhtmltoolslounge
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    da808wiz
    wrote on last edited by
    #1

    I guess this could be considered a tip as well as a possible question. I've found that if I have a table with the first row having a colspan value, i.e. header for the table, then deleting the last remaining row below the colspan row deletes the colspan row too. Check it out - paste the following into an .html, add a few rows, then delete a few rows.

    <html>
    <head><title>JS Dynamic Table Test</title>
    <script language="JavaScript">
    <!--
    function AddRow() {
    var a_misctext = ["Gary","Mary","Larry","Harry","June","Marge","Lisa","Tina","Rika","Susan"];
    tbl = document.getElementById('DataArea');
    lastrow = tbl.rows.length;
    workrow = tbl.insertRow(lastrow);
    workrow.insertCell(0);
    workrow.cells[0].innerHTML = "<nobr>"+a_misctext[Math.floor(Math.random()*10.0)]+"</nobr>";
    workrow.insertCell(1);
    workrow.cells[1].innerHTML = "<input type=\"button\" value=\"x\" onClick=\"PopRow(this.parentNode.parentNode.rowIndex);\">";

    return false;
    

    }

    function PopRow(x) {
    document.getElementById('DataArea').deleteRow(x);
    }
    //-->
    </script>
    </head>
    <body>
    <form>
    <table id=DataArea cellpadding=4 cellspacing=0 border=0>
    <tbody>
    <tr><td colspan="2">My Stuff   <input type="button" value=" Add " onClick="AddRow();"></td></tr>
    </tbody>
    </table>
    </form>
    </body>
    </html>

    I "fixed" it by turning the colspan row into 2 separate columns: <tr><td>My Stuff</td><td><input type="button" value=" Add " onClick="AddRow();"></td></tr> Any idea why the table would collapse when deleting the last row?

    S 1 Reply Last reply
    0
    • D da808wiz

      I guess this could be considered a tip as well as a possible question. I've found that if I have a table with the first row having a colspan value, i.e. header for the table, then deleting the last remaining row below the colspan row deletes the colspan row too. Check it out - paste the following into an .html, add a few rows, then delete a few rows.

      <html>
      <head><title>JS Dynamic Table Test</title>
      <script language="JavaScript">
      <!--
      function AddRow() {
      var a_misctext = ["Gary","Mary","Larry","Harry","June","Marge","Lisa","Tina","Rika","Susan"];
      tbl = document.getElementById('DataArea');
      lastrow = tbl.rows.length;
      workrow = tbl.insertRow(lastrow);
      workrow.insertCell(0);
      workrow.cells[0].innerHTML = "<nobr>"+a_misctext[Math.floor(Math.random()*10.0)]+"</nobr>";
      workrow.insertCell(1);
      workrow.cells[1].innerHTML = "<input type=\"button\" value=\"x\" onClick=\"PopRow(this.parentNode.parentNode.rowIndex);\">";

      return false;
      

      }

      function PopRow(x) {
      document.getElementById('DataArea').deleteRow(x);
      }
      //-->
      </script>
      </head>
      <body>
      <form>
      <table id=DataArea cellpadding=4 cellspacing=0 border=0>
      <tbody>
      <tr><td colspan="2">My Stuff   <input type="button" value=" Add " onClick="AddRow();"></td></tr>
      </tbody>
      </table>
      </form>
      </body>
      </html>

      I "fixed" it by turning the colspan row into 2 separate columns: <tr><td>My Stuff</td><td><input type="button" value=" Add " onClick="AddRow();"></td></tr> Any idea why the table would collapse when deleting the last row?

      S Offline
      S Offline
      sohighthesky
      wrote on last edited by
      #2

      var row=x.parentNode.parentNode
      row.parentNode.removeChild(row)
      PopRow(this)

      D 1 Reply Last reply
      0
      • S sohighthesky

        var row=x.parentNode.parentNode
        row.parentNode.removeChild(row)
        PopRow(this)

        D Offline
        D Offline
        da808wiz
        wrote on last edited by
        #3

        yup, that worked! looks like I have a lot to learn... Thanks for teaching...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups