getAttribute in DOM Level 1

Hi,

I am making DOM calls in a HTML file using Javascript. I try to search for an attribute from current element up its parent node chain, and then output all the attribute values.

It works fine in most cases, but I got an "invalid argument" error when I am at <table> element, which doesn't have "foo" as its attribute. After I added "foo" as an attr to the table element, the error disappeared. However, there are other elements who don't have "foo", but no error was thrown for them.

I am new to DOM and I couldn't figure out why it behaves this way. Can somebody give me any suggestions? What is possible to be wrong?

So the code looks like this:
for(elem = elem.parentNode ; elem.NodeType == 1 && elem ; elem = elem.parentNode )
{
     var str = elem.getAttribute("foo") ; //where invalid arguments is thrown

     if(str) {
        do something....
     }
}

Thanks.

Wei 

Received on Tuesday, 16 April 2002 20:31:01 UTC