Nick
Nick

Reputation: 1570

Cannot read property 'nodeValue' of null

I have XML:

<street></street>

or

<street>2813 Bla ave</street>

Javascript:

if ((xmldoc.getElementsByTagName('street')[i].firstChild.nodeValue != null)) {
                 alert(1);
                 }
                 alert(2);

But the script doesn't work - Cannot read property 'nodeValue' of null

Upvotes: 2

Views: 21482

Answers (4)

FlavorScape
FlavorScape

Reputation: 14319

Use xmldoc.getElementsByTagName('street')[i].innerHTML because the text you want is between the tags. I believe this IS supported for XML. Of course, you could always use the nodeValue property as well.

Upvotes: 0

Barlow Tucker
Barlow Tucker

Reputation: 6529

The street node does not have any children. You need to remove .firstChild

Upvotes: 0

RestingRobot
RestingRobot

Reputation: 2978

Your selector is failing,

 xmldoc.getElementsByTagName('street')[i].firstChild 

appears to return null. Have you tried logging and checking to make sure that the selector you want actually exists?

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324790

nodeValue will never be null because without a value the node wouldn't exist.

Remove .nodeValue from your code.

Upvotes: 3

Related Questions