View Full Version : removeChild() not removing <img />

12-02-2004, 10:00 PM
I've made good progress with a script I'm writing that will link <q />s to their cite attribute URI, but I've hit yet another roadblock...

To link to the cite URI of <q /> elements, I want to basically wrap the existing quote text in an <a />. To do this, I thought it would be easy enough to capture the original <q /> text in a variable, then drop all child nodes of the <q /> and replace them with the newly constructed <a /> that contains the original <q /> text.

I've got this working fine, but I'm having issues with the recognition of child nodes that are <img /> elements. In WordPress, for example, certain smilies are converted to images. If a smiley is used in a quote, I want to grab the value of the <img />'s alt attribute before dumping the <img /> element as well as any other text nodes.

I'm not getting the <img />'s alt value in my variable holding the original <q /> text, and the <img /> elements aren't being dropped either.

An example page here: http://sethrasmussen.com/common/js/q-child-nodes.html

The script here: http://sethrasmussen.com/js/_exit.js


I cross posted this from my other thread because I thought the specific title might get better attention. I've googled ad nauseum, etc. I can't seem to find anything pointing to why removeChild might not be working in this instance.

12-02-2004, 10:22 PM
It seems that, in Firefox, the following structure:

<q>Text<img /></q>

Is showing two #text nodes as the children of the <q /> element, instead of one #text and one element node.

Even still, I don't follow why the actual #text node is being removed, but not the <img /> element's node.

EDIT: Discovered that the second #text node was due to white space. Still haven' figured why the <img /> isn't being removed.

12-03-2004, 02:59 PM
Yes, Moz counts even the empty textNodes as childs.

a simple crossbrowser solution to remove all the childs


12-03-2004, 04:27 PM
hrm... but it seems that, for one, the <img /> isn't even being recognized as a child, or it is, but as a #text node.

regardless, i am having other text nodes removed using a for loop to loop over the length of elem.childNodes.

what would be different about your loop other than syntax?

also, without the index of a for loop, it's more tedious to check each child for what type of node it is. if you checked my script, if the child is an <img /> i want to get it's alt attribute value before i drop it, whereas i just grab the nodeValue of text nodes.

12-03-2004, 05:52 PM
hm... IMG might not need a end tag... the code I have given your works always for me, but I dont use <img /> for inserting images (on the other hand, if trying a innerHTML after a createElement IMG you might see that DOM don't insert new elements as <tag /> but as <tag></tag> if tag needs that)

I did not understand quite well your other problems, yet... if you want to only remove a certain child you may try find that child element by its tag


if they are more than one, loop