views:

848

answers:

4

How can I delete all the tables in a web page? The tables don't have any ids associated with them.

+2  A: 

If you're using jQuery it is pretty easy ...

$(document).ready(function() {
  $("table").remove();
});

not sure how's you do it in other libraries.

if you're not using a js library, you should be.

Kyle West
This will do it when the document loads. If you want to do it later, use $('table').remove() in an event handler.
tvanfosson
@tvanfosson: Could you please elaborate it a bit? Also I'm using wordpress blog? How can I load jquery library?
askgelal
Why use a library of thousands of lines if you have a need for removing some tables ?-) As Joel Coehoorn shows it is only three lines of code, though they are some longer as they would be using jQuery ...
roenving
Agreed. It's the StackOverflow disease, you can't ask a simple JavaScript question without the fanboys jumping in to hype up their favourite framework. Please stop. Frameworks can be great, sure, but no-one's going to pull in that insane weight of code for a one-liner.
bobince
If you're already using a framework, seeing how to do it with that is useful. I'm fine with those kinds of answers.
Ben Combee
The jQuery library is 15kb .. hardly massive. Furthermore, I've never seen a web application that has just one use of javascript. Sure in this example it may be just one example, but I'd put money on the next couple of questions will be, OK, tables are done, how do I do x?
Kyle West
I wish I could upvote this answer more than once. :)
Brad Wilson
I think having a library approach in addition to pure JS approaches is constructive.
dshaw
No fanboy here. Just a rational coder. It's insane to NOT use jQuery. It doesn't only do things better, it does them better across browsers.
rp
+5  A: 

Very simple version:

var tables = document.getElementsByTagName("TABLE");
for (var i=tables.length-1; i>=0;i-=1)
   if (tables[i]) tables[i].parentNode.removeChild(tables[i]);
Joel Coehoorn
This will miss alternate tables — see comment below for the gories.
bobince
lol, yeah. I'll fix it.
Joel Coehoorn
+4  A: 

Danger! getElementsByTagName returns a ‘live’ NodeList. In Joel's code, removing element 0 moves the items in the list down so that when you remove element 1, you've missed one.

Possible alternatives: if you know you're always going to be removing every element, you can use a while-loop:

var tables= document.getElementsByTagName('table');
while (tables.length>0)
    tables[0].parentNode.removeChild(tables[0]);

Or, if you might or might not remove, but iteration order is unimportant, go through the list backwards:

var tables= document.getElementsByTagName('table');
for (var i= tables.length; i-->0;)
    tables[i].parentNode.removeChild(tables[i]);

If you might-or-might-not remove and you need to iterate forwards, you're in the tedious position of having to copy the list:

function toArray(l) {
    var a= [];
    for (var i= 0; i<l.length; i++)
        a[i]= l[i];
    return a;
}

var tables= toArray(document.getElementsByTagName('table'));
for (var i= 0; i<tables.length; i++)
    ...
bobince
A: 

Or:

function myF() {
    this.checkChild = function(tagN, node) {
     if (node.tagName.toLower() == tagN.toLower()) {
      node.parentNode.removeChild(node);
     }
     else {
      var i;
      for(i = 0; i < node.childNodes.length; i++)
       this.checkChild(tagN, node.childNodes[i]);
     }
    }
}

Usage:


var m = new myF();
m.checkChild("The name of the tagname. This case: table", document.body); 
 

Good luck!

José Leal