2

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

0

4 Answers 4

12

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]);
Sign up to request clarification or add additional context in comments.

1 Comment

This will miss alternate tables — see comment below for the gories.
5

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++)
    ...

Comments

3

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.

8 Comments

This will do it when the document loads. If you want to do it later, use $('table').remove() in an event handler.
@tvanfosson: Could you please elaborate it a bit? Also I'm using wordpress blog? How can I load jquery library?
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 ...
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.
If you're already using a framework, seeing how to do it with that is useful. I'm fine with those kinds of answers.
|
1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.