Connecting Tech Pros Worldwide Help | Site Map

html DOM : unable to insertBefore a DL node.

e michael brandt
Guest
 
Posts: n/a
#1: Feb 10 '06
I am frustrated. It appears that one can not use insertBefore to insert
an A node before a DL node. Is that really true?

I *am* able to insert before a <p id="hh"> tag, but not before a <dl
id="hh"> tag.

i am able to do a body.appendChild with no trouble.

Finally, winIE6 pops an error: 'Invalid argument' at the last snip line
below (FF1.5 fails silently):

A snip from my code:

****
var newA = document.createElement("A");

var refObj=document.getElementById("hh");

//document.body.appendChild(newA); //works fine
document.body.insertBefore(newA,refObj);// winIE6 error: 'Invalid argument'
****

Any ideas why this is happening Is it me? or is there some rule I've
not been able to find?

Thanks for any help.

P.S. Same problem if I use "div1", a div wrapper around the dl node,
instead of body node.

emichael b.
Martin Honnen
Guest
 
Posts: n/a
#2: Feb 10 '06

re: html DOM : unable to insertBefore a DL node.




e michael brandt wrote:

[color=blue]
> A snip from my code:
>
> ****
> var newA = document.createElement("A");
>
> var refObj=document.getElementById("hh");
>
> //document.body.appendChild(newA); //works fine
> document.body.insertBefore(newA,refObj);// winIE6 error: 'Invalid argument'[/color]

You want/need
if (refObj != null) {
refObj.parentNode.insertBefore(newA, refObj);
}
You always need to call the DOM methods insertBefore and appendChild on
the parent node you want to append to or insert into.

--

Martin Honnen
http://JavaScript.FAQTs.com/
e michael brandt
Guest
 
Posts: n/a
#3: Feb 10 '06

re: html DOM : unable to insertBefore a DL node.


wow! That works perfectly. I read every google hit I could find and
never did I see this. Thanks so much!

emichael

Martin Honnen wrote:[color=blue]
>
>
> e michael brandt wrote:
>
>[color=green]
>> A snip from my code:
>>
>> ****
>> var newA = document.createElement("A");
>>
>> var refObj=document.getElementById("hh");
>>
>> //document.body.appendChild(newA); //works fine
>> document.body.insertBefore(newA,refObj);// winIE6 error: 'Invalid
>> argument'[/color]
>
> You want/need
> if (refObj != null) {
> refObj.parentNode.insertBefore(newA, refObj);
> }
> You always need to call the DOM methods insertBefore and appendChild on
> the parent node you want to append to or insert into.
>[/color]
Closed Thread