PDA

View Full Version : [resolved]Firefox has an imaginary node?


johnnyb
09-21-2006, 09:40 PM
Hi,
I'm writing a script where the user clicks a button and a function is called that clones the last child of the node right before the button. Here's the function, (it's a bit funky for debugging):

function anothervideo()
{
var marker = document.getElementById("addmore");
var prev=marker.previousSibling;
var preprev = prev.previousSibling;

var parent=marker.parentNode;
var newvid = prev.cloneNode(true);

alert(prev.tagName);

//parent.insertBefore(newvid, marker);

}


And here's the HTML around the button:

</div>
</fieldset>
</li>
</ol>
<input type="button" value="Add Another Video" onclick="anothervideo();" id="addmore"/>


This is only the excerpt leading up to the button. The rest of my XHTML is correct and valid - I checked.

So, as it stands now when the button is clicked it should alert 'OL' but instead it alerts 'undefined'. When I alert(typof prev) it says it's an object.

If I alert preprev.tagName, (the sibling before the one before the button), it echos 'OL'! It seems that Firefox thinks there is an extra element in between the <ol> and the <input type="button">.

It works as expected in IE6 but I need it to run cross-browser.

Has anyone else experienced this and does anyone know how to fix it?

John

Resolved: There was a line break before the button that FF was treating as a text node.

liorean
09-22-2006, 05:01 PM
Well, you would probably get your answer if you looked at the node tree in the DOM Inspector. But oh well, I could as well tell you: The previousSibling is a text node containing a line break. Because that's how the parse tree looks in a sensible browser.

Test the previousSibling for what nodeType it is. If not element, go to the previousSibling of that node, and so on.

Ah, you already figured that out, I see