<html>
<head>
<script type="text/javascript" src="jQuery.js">
<script type="text/javascript">
x=document.getElementByTagName("p");
document.write(x.lastChild.nodeValue);
</script>
</head>
<body>
<p id="intro">Hello World 1!</p>
<p id="intro">Hello World 2!</p>
<p id="intro">Hello World 3!</p>
</body>
</html>
Why doesn’t the above code work. I want to show [Hello World 3!] by using the statement documetn.write(x.lastChild.nodeValue());
Thanks in advance…
There are several errors in your code:
<p>are not existent at the time you want to query them.Simple solution would be to move your
<script>tag to the bottom of the HTML page.document.getElementsByTagName()(and notdocument.getElementById()– note the added “s”) and returns an array.So here the correct syntax for you would be something like this:
document.write()? This inserts text at the very position of the script tag.So for debugging it would be better to use
console.log()oralert(). In a productive environment, you would have a result-<div>for example.So finally your code maybe could look like this in order to work: