firstChild not working in Netscape 7.1??

P

Prasanna

Friends,

the Javascript function firstChild() works as expected in IE but
returns "undefined" in Netscape 7.1

Here is the sample html code that I used. I am asking the javascript
to print out the id of the child of para1. IE correctly prints the
string "italicsid", but Netscape gives me an "undefined". Why so? Any
help is greatly appreciated.

Thanks,
Prasanna


<html>
<head>
<script LANGUAGE="Javascript">

function GetFirstChild(which)
{
curr = document.getElementById(which);
{
alert(curr.firstChild.id) ;
}
}

</script>

</head>
<body>
<p class=insert id=para1 onclick="GetFirstChild('para1')">
<i id="italicsid" name="italicsname">
Child of para1
</i>
</p>
</body>
</html>
 
M

Martin Honnen

Prasanna said:
the Javascript function firstChild() works as expected in IE but
returns "undefined" in Netscape 7.1

firstChild is not a function, neither with IE nor with Netscape.
Here is the sample html code that I used. I am asking the javascript
to print out the id of the child of para1. IE correctly prints the
string "italicsid", but Netscape gives me an "undefined". Why so? Any
help is greatly appreciated.

In Netscape's DOM white space text nodes are modelled while IE throws
them out, therefore with Netscape the first child node is a text node
and not an element node, and text nodes don't have an id property which
is why you get undefined for the id.
You need to check whether
node.nodeType == 1
to find out whether something is an element node.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top