Javascript Remove Row From Table

Here’s a short function to remove a row from a table using the row’s id

function removeRow(id) {
  var tr = document.getElementById(id);
  if (tr) {
    if (tr.nodeName == 'TR') {
      var tbl = tr; // Look up the hierarchy for TABLE
      while (tbl != document && tbl.nodeName != 'TABLE') {
        tbl = tbl.parentNode;
      }
 
      if (tbl && tbl.nodeName == 'TABLE') {
        while (tr.hasChildNodes()) {
          tr.removeChild( tr.lastChild );
        }
      tr.parentNode.removeChild( tr );
      }
    } else {
      alert( 'Specified document element is not a TR. id=' + id );
    }
  } else {
    alert( 'Specified document element is not found. id=' + id );
  }
}
Related Posts Plugin for WordPress, Blogger...
  1. 10 Responses to “Javascript Remove Row From Table”

  2. Very Good! Many Thanks!!! Bye

    By Diego on Mar 20, 2009

  3. my pleasure dude

    By tech on Apr 27, 2009

  4. How bout pairing this script with one that ads a row instead of deleting?

    By Dexter on Dec 17, 2009

  5. i will have to look the code for that one.

    By tech on Dec 17, 2009

  6. This is perfect! Thanks for the clear article!

    By Klom Dark on Feb 18, 2010

  7. you’re welcome ;)

    By tech on Feb 18, 2010

  8. This is a very exciting post, I was looking for this information. Just so you know I found your site when I was doing research for blogs like mine, so please check out my site sometime and leave me a comment to let me know what you think.

    By Markita Palmerino on Apr 11, 2010

  9. Thanks for this awesome source! ;)

    By Edward Aum on Aug 13, 2010

  10. @edward: you’re welcome ;)

    By tech on Aug 13, 2010

  11. Awesome :) Works lik magic!
    Was looking for something lik this

    By Shruti on Sep 2, 2011

Post a Comment

Anti-Spam Protection by WP-SpamFree

Spam protection by WP Captcha-Free