2
var objects = document.getElementsByTagName('object');
for (var i=0, n=objects.length;i<n;i++) {
    objects[i].style.display='none';
    var swfurl;
    var j=0;
    while (objects[i].childNodes[j]) {
    if (objects[i].childNodes[j].getAttribute('name') == 'movie') {
            /* DO SOMETHING */ 
    }
    j++;
}
    var newelem = document.createElement('div');
    newelem.id = '678297901246983476'+i;
    objects[i].parentNode.insertBefore(newelem, objects[i]);
    new Gordon.Movie(swfurl, {id: '678297901246983476'+i, width: 500, height: 400});
}

It says that getAttribute is not a function of childNodes[j]. What's wrong? I don't see the point.

3 Answers 3

8

Remember that childNodes includes text nodes (and comment nodes, if any, and processing instructions if any, etc.). Be sure to check the nodeType before trying to use methods that only Elements have.

Update: Here in 2018, you could use children instead, which only includes Element children. It's supported by all modern browsers, and by IE8-IE11. (There are some quirks in older IE, see the link for a polyfill to smooth them over.)

Sign up to request clarification or add additional context in comments.

4 Comments

dang, beat me to it. This is a common issue, since indenting and tidying HTML introduces text nodes that contain only white-space, with one of these text nodes being the first child of the parent element. You can also use children to get child elements only. +1
@Andy E: children isn't yet quite well-supported enough to use on the general web, I'd say.
In 2018, using .children is fine.
@Xufox - Indeed. Seven years. Yikes. How long have I been on this site? (rhetorical)
6

Check the nodeType property is 1 (meaning the node is an element) before calling element-specific methods such as getAttribute(). Also, forget getAttribute() and setAttribute(): you almost never need them, they're broken in IE and they don't do what you might think. Use equivalent DOM properties instead. In this case:

var child = objects[i].childNodes[j];
if (child.nodeType == 1 && child.name == 'movie') {
    /* DO SOMETHING */ 
}

Comments

-5

What browser are you using ? If it's IE then you need to use readAttribute instead.

4 Comments

This isn't correct at all. Internet Explorer has getAttribute, it just doesn't work properly pre IE 8. It should still work in this case.
Well I assumed he's not targeting only IE8 or above. But I kinda didn't know that it actually works in IE8 - I always used readAttribute for that kind of browser :-D
there is no readAttribute method in any version of Internet Explorer. It's always been getAttribute. See for yourself: msdn.microsoft.com/en-us/library/ms533053(v=VS.85).aspx
My bad, you're right. I probably mixed it up with the one available in the Prototype JS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.