BooKwiznak Posted March 20, 2011 Share Posted March 20, 2011 Hello forum,Not sure if this is the right place to ask this question, but I was going through the HTML DOM tutorials and came on a 'road block'. I'm in the Node Access section and the last example where they mention using the firstChild property is confusing me a bit. The following example code is : <html><body><p id="intro">Hello World!</p><script type="text/javascript">x=document.getElementById("intro");document.write(x.firstChild.nodeValue);</script></body></html> And the output would be: Hello World!Now here is my code: <html><body><p>Hello World!</p><p>Bye World!</p><script type="text/javascript">x=document.getElementsByTagName("p");document.write(x.lastChild.nodeValue);</script></body></html> I'm trying to get the output to be: Bye World! The code doesn't work. Am I missing something? Link to comment Share on other sites More sharing options...
jeffman Posted March 21, 2011 Share Posted March 21, 2011 getElementsByTagName returns a node list (like an array), not a node, so it doesn't have children.document.getElementsByTagName("p")[1] refers to the second p element. Link to comment Share on other sites More sharing options...
BooKwiznak Posted March 21, 2011 Author Share Posted March 21, 2011 getElementsByTagName returns a node list (like an array), not a node, so it doesn't have children.document.getElementsByTagName("p")[1] refers to the second p element.Ah! Got it! Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.