tags:

views:

380

answers:

2

Hey,

I've got a div with two children, one <h2> and one <ul>.

My problem is that when I check .length of my div, by doing document.getElementById('wrap').childNodes.length, I get 5 instead of 2. And if I run console.log on 'childNodes' I get this:

[<TextNode textContent="\n ">, h2, <TextNode textContent="\n ">, ul, <TextNode textContent="\n ">]

My HTML looks like this:

<div id="wrap">
    <h2>Lorem</h2>
    <ul>
        <li>Lorem</li>
        <li>Ipsum</li>
    </ul>
</div>

Indenting my code has never before effected the length of the childNodes.

If I type everything on one row (as one long word) I get rid of all <TextNode> and it counts to 2 as it should and not 5.

I've got No idea what's wrong, but I figured I need to remove all empty textnodes. Which way would be the best to do this?

Thanks heaps

Ps. These <TextNode textContent="\n "> occurs all over the document, so I need to remove them all, not only in this particular div

+1  A: 

You don't have to remove any extra text nodes (although there are time when you should - for performance reasons). You should always check for the NodeType when viewing the property ChildNodes.

Here is a link that should help:

http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247

EDIT:

Here is a page that has some prototype code that should help:

http://v3.thewatchmakerproject.com/journal/329/finding-html-elements-using-javascript-nextsibling-and-previoussibling

Here is the code from the link above:

Object.prototype.nextObject = function() {
    var n = this;
    do n = n.nextSibling;
    while (n && n.nodeType != 1);
    return n;
}

Object.prototype.previousObject = function() {
    var p = this;
    do p = p.previousSibling;
    while (p && p.nodeType != 1);
    return p;
}
Gabriel McAdams
well, i've used .lastChild before without any problems, and now, all of a sudden, instead of returning the actual last child, it gives me an empty textnode?so instead of using .childNodes like i've always been doing, ur saying i should now also check the nodetype? this just feels so werid since childnodes always has returned the same in both ie and ff. now i will have to do a check to see if its ff or ie sort of :-/
meow
Check the second link I provided. It contains code that adds a function onto Object that allows you to filter the nodes by type.
Gabriel McAdams
@Isabell: This isn't something that changed recently. This behavior has been in several browsers for a long time, and is completely valid per the spec.
Daniel Pryden
Remember to accept this answer if it solved your problem
Gabriel McAdams
+1  A: 

Firefox, right? Firefox will treat whitespace as a textnode. There's nothing you can do about it apart from code around them. Or, you could completely get rid of all whitespace, but I'm betting that's not an option. It's part of what makes writing webpages so, er, fun :-)

Dan F
i tested firefox and safari, theyre the same = counts 5ie = 2.youd think that eventho it treats them as textnodes, they shouldnt be counted as an actual childnode, but instead just be ignored
meow
Yep. At least some consistency would be nice either way. Normally I shy away from saying "use X framework", but they can help smooth out these weird wrinkles. If it's a massive problem, try jQuery or prototype or moo or one of those big ones. Otherwise, a bit of nodeType checking should get you sorted again.
Dan F