It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

noob with DOM problem

noob with DOM problem

Postby captain_salad on Tue Feb 19, 2008 2:13 pm

I am just learning the DOM and thought I understood it until I tried my scripts in firefox/opera.

In IE the simple script below returns an alert with the ID of the first child node, in opera/firefox I get an alert with "undefined". Can ANYONE tell me why as I am way to dumb to work out why! :)

script below

<script type="text/javascript">

function getId(){
if (document.childNodes) {
var obj = document.getElementById("container")
alert(obj.childNodes[0].id)
}
}

</script>

<body onLoad="getId()">

<div id="container">
<div id="black">BLACK</div>
<div id="blue">BLUE</div>
</div>
</body>
captain_salad
 
Posts: 2
Joined: Tue Feb 19, 2008 1:53 pm

Postby leonard on Tue Feb 19, 2008 4:15 pm

You're not dumb at all.

IE and FF have a different implementation of childNodes[].
FF counts all blanks whereas IE skips them.

This is stupid, I know... I have stumbled over this problem myself.
I just try to avoid using childNodes[] and if you really need that, you probably need to check the nodeType and continue the array until you have your correct object.

cheers!
- leonard
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 7:11 am
Location: Switzerland

Postby captain_salad on Tue Feb 19, 2008 8:06 pm

Thanks for the reply I really appreciate it! Sounds annoying, so note to self avoid using child nodes. I tried using nextSibling (see code below) and got an “undefined” alert again. Is there a problem with nextSibling in FF as well?

Thank you for your time and help!


<script type="text/javascript">
function getId(){
var obj = document.getElementById("black")
alert(obj.nextSibling.id)
}

</script>



<body onLoad="getId()">
<div id="container">
<div id="black">BLACK</div>
<div id="blue">BLUE</div>
</div>
</body>
captain_salad
 
Posts: 2
Joined: Tue Feb 19, 2008 1:53 pm


Who is online

Users browsing this forum: No registered users and 5 guests