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
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
Share Improve this question asked Apr 17, 2012 at 16:44 NickNick 1,5702 gold badges16 silver badges23 bronze badges 3- You should give more details, like the full XML and the full code! – gdoron Commented Apr 17, 2012 at 16:45
-
.firstChild
is null, so -xmldoc.getElementsByTagName('street')[i]
has no children. – c69 Commented Apr 17, 2012 at 16:46 - @Kolink I'm assuming his javascript resides in some sort of loop. – RestingRobot Commented Apr 17, 2012 at 16:48
4 Answers
Reset to default 3nodeValue
will never be null
because without a value the node wouldn't exist.
Remove .nodeValue
from your code.
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?
The street node does not have any children. You need to remove .firstChild
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.