Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-02-2004, 09:00 PM   PM User | #1
]|V|[agnus
Regular Coder

 
Join Date: May 2004
Location: Minneapolis, MN, USA
Posts: 904
Thanks: 0
Thanked 0 Times in 0 Posts
]|V|[agnus is an unknown quantity at this point
removeChild() not removing <img />

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.
__________________

Opposite of Sequitur
]|V|[agnus is offline   Reply With Quote
Old 12-02-2004, 09:22 PM   PM User | #2
]|V|[agnus
Regular Coder

 
Join Date: May 2004
Location: Minneapolis, MN, USA
Posts: 904
Thanks: 0
Thanked 0 Times in 0 Posts
]|V|[agnus is an unknown quantity at this point
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.
__________________

Opposite of Sequitur

Last edited by ]|V|[agnus; 12-02-2004 at 09:24 PM..
]|V|[agnus is offline   Reply With Quote
Old 12-03-2004, 01:59 PM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Yes, Moz counts even the empty textNodes as childs.

a simple crossbrowser solution to remove all the childs

while(element.hasChildNodes()){
element.removeChild(element.childNodes[0])
}
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 12-03-2004 at 02:01 PM..
Kor is offline   Reply With Quote
Old 12-03-2004, 03:27 PM   PM User | #4
]|V|[agnus
Regular Coder

 
Join Date: May 2004
Location: Minneapolis, MN, USA
Posts: 904
Thanks: 0
Thanked 0 Times in 0 Posts
]|V|[agnus is an unknown quantity at this point
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.
__________________

Opposite of Sequitur

Last edited by ]|V|[agnus; 12-03-2004 at 03:32 PM..
]|V|[agnus is offline   Reply With Quote
Old 12-03-2004, 04:52 PM   PM User | #5
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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

element.getElementsByTagName('tag')[0]

if they are more than one, loop
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:39 AM.


Advertisement
Log in to turn off these ads.