Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove All Images

Tags:

javascript

What JavaScript will remove all image tags?

like image 878
steven Avatar asked Sep 17 '09 08:09

steven


People also ask

What is the command to remove all the images at once?

Remove all images All the Docker images on a system can be listed by adding -a to the docker images command. Once you're sure you want to delete them all, you can add the -q flag to pass the image ID to docker rmi : List: docker images -a.

How do I delete all containers and photos?

Remove all Containers: To remove all containers from the docker-machine, we need to get the ids of all the containers. We can simply get the ids of the containers with the command docker ps -aq, then by using the docker rm command, we can remove all the containers in the docker-machine.

How do I clean up docker images?

To clean this up, you can use the docker container prune command. By default, you are prompted to continue. To bypass the prompt, use the -f or --force flag. Other filtering expressions are available.


2 Answers

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]);
like image 130
bobince Avatar answered Nov 08 '22 08:11

bobince


UPD: sorry, this is a wrong answer, see comments. This is a correct answer.

Something like this:

images = document.getElementsByTagName('img');
for (i = 0; i < images.length; i++) {
    images[i].parentNode.removeChild(images[i]);
}

OR a slight modification of my first attempt to answer this question:

var images = document.getElementsByTagName('img');
var l = images.length;
for (var i = 0; i < l; i++) {
    images[0].parentNode.removeChild(images[0]);
}
like image 26
n1313 Avatar answered Nov 08 '22 09:11

n1313