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
2008-11-15 03:29:24
This will do it when the document loads. If you want to do it later, use $('table').remove() in an event handler.
tvanfosson
2008-11-15 03:32:06
@tvanfosson: Could you please elaborate it a bit? Also I'm using wordpress blog? How can I load jquery library?
askgelal
2008-11-15 03:40:39
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
2008-11-15 09:08:23
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
2008-11-15 12:13:11
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
2008-11-15 18:50:29
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
2008-11-15 21:33:06
I wish I could upvote this answer more than once. :)
Brad Wilson
2008-11-16 04:57:50
I think having a library approach in addition to pure JS approaches is constructive.
dshaw
2008-11-16 05:16:45
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
2008-11-16 05:54:44
+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
2008-11-15 03:35:08
+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
2008-11-15 12:10:48
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
2008-11-16 04:00:50