What JavaScript will remove all image tags?
Do I have to include the whole library for just a simple task?
steven
2009-09-17 08:41:01
+1 jQuery... no need to reinvent the wheel. :-)
beggs
2009-09-17 08:46:39
@beggs: I disagree. Giving a JQuery answer when there is no indication that the JQuery framework is in use, is like giving a .NET based answer to C++ question. If you are using it great and its ok as a comment that it may be woth considering but its not an answer to a non-Jquery question. n1313 answer is hardly rocket science and adding a large framework .js to your page so that you don't "reinvent the wheel" just doesn't make sense.
AnthonyWJones
2009-09-17 09:08:53
-1: No point downloading a 55.9KB library for a trivial DOM manipulation task.
NickFitz
2009-09-17 09:15:34
jQuery is really hardly 'a whole library'. it's a 56k script file - not really so 'large'. maybe overkill to do this one remove-images job. if this is really the *only* thing being done, but otherwise is pretty lightweight to use. i consider jQuery answers to javascript questions totally appropriate.
Scott Evernden
2009-09-17 09:16:42
besides which see @bobince below. the jQuery answer is correct and the one getting upvoted has a bug
Scott Evernden
2009-09-17 09:19:50
@NickFitz - how do you know it'll need to be downloaded? (a) The OP *may* already be using jQuery, in which case this is simpler than the non-jQuery alternatives, and (b) There's always the google-hosted version which users may well have cached.
Dominic Rodger
2009-09-17 09:24:06
Recommended improvement: use 'var' to properly declare 'images' and 'i'.
bobbymcr
2009-09-17 08:46:49
As bobince points out, this answer fails to take account of the fact that the NodeList returned by getElementsByTagName is live..
NickFitz
2009-09-17 09:13:43
once again the 'accepted correct' answer is wrong here at SO .. Aaarrgh
Scott Evernden
2009-09-17 09:21:05
-1 for being the wrong answer. It'd be nice if the OP would change his accepted answer so other users don't fall into this trap.
Jamie Dixon
2009-09-17 09:39:10
Sorry, guys, I totally forgot about that. I will edit my answer to reflect this.
n1313
2009-09-17 10:12:03
Doesn't the edited version remove only half of the NodeList as well? But this time not every other Node, just second half of the list. You're increasing i until it reaches images.length, which is reduced in every iteration, so they kind of "meet" in the middle. Store the length outside the loop and it will be fine then.
macbirdie
2009-09-17 17:43:26
A:
Without using external libraries:
var images = document.getElementsByTagName('img');
for(var i=0; i < images.length; i++) {
images[i].parentNode.removeChild(images[i]);
}
Or using jquery:
$('img').remove();
Keeper
2009-09-17 08:47:16
+12
A:
The previous answer will only remove every second image.
Remember NodeLists returned by getElementsByTagName or other DOM methods are ‘live’. That means when you remove image 0, images 1–n move down to 0–(n-1); this is a ‘destructive iteration’.
To avoid this, either make a static Array copy of the NodeList (as the jQuery answer is effectively doing), or, faster, just iterate the list backwards:
for (var i= document.images.length; i-->0;)
document.images[i].parentNode.removeChild(document.images[i]);
bobince
2009-09-17 09:09:36
+6
A:
This should work too:
var images = document.getElementsByTagName('img');
while(images.length > 0) {
images[0].parentNode.removeChild(images[0]);
}
macbirdie
2009-09-17 09:24:52
Yep, this is a good approach to destructive iteration, when you know you're going to remove *every* item.
bobince
2009-09-17 09:37:33
This is the best answer, because the while syntax combined with the live NodeList is more clear.
eyelidlessness
2009-09-17 17:49:13