Skip to content
Advertisement

Javascript – getElementsByTagName returns undefined for some elements

I’m parsing an XML via Javascript and I try to print out specific elements in HTML. I use the method getElementsByTagName and it seems to be working for some elements, but for others it returns undefined, even though the element name is written correctly. Here is a code snippet from the XML file:

JavaScript

I created a table in HTML to extract the values from Cloudiness, Humidity, MinTemp and MaxTemp, which are in this case “Very cloudy”, “Very humid”, 3 and 30. The table in HTML looks like this:

JavaScript

And in JS, I update the innerHTML of the table cells by doing this:

JavaScript

And for some reason, it works fine for Cloudiness and Humidity (I get the correct values “Very cloudy” and “Very humid”). But for MinTemp and MaxTemp, I always get an undefined error app.js:57 Uncaught TypeError: Cannot read property 'textContent' of undefined. Does anyone know why I get this error? Would appreciate all kinds of help!

Advertisement

Answer

Looks like in the xml document, the tag is not closed properly.

JavaScript

The last tag above is not a closure tag – may be a typo ?

PS: There are others below with the same issue, please fix them as well.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement