tags:

views:

308

answers:

5
+2  Q: 

Remove All Images

What JavaScript will remove all image tags?

+3  A: 

With jQuery:

$("img").remove();
Dominic Rodger
Do I have to include the whole library for just a simple task?
steven
+1 jQuery... no need to reinvent the wheel. :-)
beggs
@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
-1: No point downloading a 55.9KB library for a trivial DOM manipulation task.
NickFitz
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
besides which see @bobince below. the jQuery answer is correct and the one getting upvoted has a bug
Scott Evernden
@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
+5  A: 
n1313
Recommended improvement: use 'var' to properly declare 'images' and 'i'.
bobbymcr
This doesn't work — see explanation below.
bobince
As bobince points out, this answer fails to take account of the fact that the NodeList returned by getElementsByTagName is live..
NickFitz
once again the 'accepted correct' answer is wrong here at SO .. Aaarrgh
Scott Evernden
-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
Sorry, guys, I totally forgot about that. I will edit my answer to reflect this.
n1313
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
*Cries in despair*
n1313
*Hugs n1313*
macbirdie
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
I was ninja'd ;)
Keeper
+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
An answer that works, let's hope steven updates the accepted answer to reflect this.
+6  A: 

This should work too:

var images = document.getElementsByTagName('img');
while(images.length > 0) {
    images[0].parentNode.removeChild(images[0]);
}
macbirdie
Yep, this is a good approach to destructive iteration, when you know you're going to remove *every* item.
bobince
This is the best answer, because the while syntax combined with the live NodeList is more clear.
eyelidlessness