Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Chrome Extension Remove Element

I am trying to remove an element from a div on a third party website.

Such as

<div id="full_size_photo">
  <img src="img1">
  <img src="img2">
</div>


var imageDiv  = document.getElementById("full_size_photo");
imageDiv.removeChild(imageDiv.childNodes[i]);

imageDiv apparently has 5 children? :S When i is 1 img1 is removed properly. When i remove 3 and 4 img2 is removed...

Would someone be able to explain why this is?

As far as I understand I thought the first img tag would be 0 and the second would be 1?

like image 301
John O'Fish Avatar asked Feb 05 '26 16:02

John O'Fish


1 Answers

Whitespace stretches are textNode instances

Annotated example:

<div id="full_size_photo">[node 1 --
--]<img src="img1">[node 3 --
--]<img src="img2">[node 5 --
]</div>

Here's what you really want to do:

var div = document.getElementById("full_size_photo");
var images = div.getElementsByTagName("img");
div.removeChild(images[0]);

Hope this helps!

like image 123
jimbo Avatar answered Feb 12 '26 07:02

jimbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!