...

View Full Version : can't get 'nextSibling' to work



7stud
01-19-2005, 08:48 AM
This works for me in IE6, but not FF1.0:

(Alert! This question is cross posted here :
http://www.webdeveloper.com/forum/showthread.php?s=&threadid=54082
but an answer doesn't seem to be forthcoming.)



<html>
</style>
<script type="text/javascript" language="javascript">
<!-- Hide from browsers without javascript

window.onload=function()
{
alert(document.getElementById("d1").nextSibling.id);
};

// End hiding -->
</script>
</head>
<body>

<div id="d1">text1</div>
<div id="d2">text2</div>

</body>
</html>

Kor
01-19-2005, 09:57 AM
It will work in Moz also if you remove the empty space like this:

<div id="d1">text1</div><div id="d2">text2</div>

The problem is that Moz (otherwise a correct interpretation) sees the possible textNode (even if an empty space)as the nextSibling, not the next <div> (as IE does)

Search the Forum for a code (I remeber I've seen it somewere) to "remove" empty spaces for Moz.

Or use the reference by tag name rather than firstChild or nextSibling...

7stud
01-19-2005, 01:12 PM
Someone on the other forum posted what the problem was: Moz sees a carriage return between two <divs> like this:

<div>some text</div>(invisible carriage return)
<div>other text</div>

so the nextSibling of the first <div> is a text node containing the invisible carriage return, which is a pretty ridiculous implementation in my opinion.

Kor
01-19-2005, 01:29 PM
it is the same thing as I said, but in other words. No, it is not really a silly implementation, it is an exact implementation of w3c recomandations. Moz keeps the ideea that before each tag child of a parent may be inserted a textNode, thus the order is

parent
textNode0
tagChild0
textNode1
tagChild1
textNode2
tagChilde2
/parent

This case, you see that parent.firstChild is the textNode0, not the tagChild0, even if the textNode is empty (in fact an empty space) . The same with parent.tagChild0.nextSibling, which is textNode1, not tagChild1, even if the textNode is empty.

jkd
01-19-2005, 04:34 PM
Spurious text node generation preserves indentation in the DOM representation. Try serializing a DOM in IE, then do the same thing in Mozilla. You'll notice Mozilla's is actually readable.

brothercake
01-19-2005, 06:14 PM
Here's the code to get rid of them, if you need to -- http://www.codingforums.com/showthread.php?t=7028http://www.codingforums.com/showthread.php?t=7028



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum