Koliya
Koliya

Reputation: 29

How do I remove an element from the DOM, given its id?

In this specific case, the element is a table row.

Upvotes: 2

Views: 307

Answers (4)

Pranay Rana
Pranay Rana

Reputation: 176946

Jquery

$('#myTableRow').remove();

This works fine if your row has an id, such as:

<tr id="myTableRow"><td>blah</td></tr>

Pure Javascript :

Javascript Remove Row From Table

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 &amp;&amp; tbl.nodeName != 'TABLE') {
        tbl = tbl.parentNode;
      }

      if (tbl &amp;&amp; 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 );
  }
}

Upvotes: 0

Bobby Jack
Bobby Jack

Reputation: 16049

var row = document.getElementById("row-id");
row.parentNode.removeChild(row);

Upvotes: 4

Brock Adams
Brock Adams

Reputation: 93553

var zTag = document.getElementById ('TableRowID');
zTag.parentNode.removeChild (zTag);

Or in jQuery:

$('#TableRowID').remove ();

Upvotes: 2

Fermin
Fermin

Reputation: 36111

Untested but something like:

var tbl = document.getElementById('tableID');
var row = document.getElementById('rowID');
tbl.removeChild(row);

or

var row = document.getElementById('rowID');
row.parentNode.removeChild(row);

Upvotes: 6

Related Questions