In my page I've got 3 table elements, I want to access the 3rd element using its tagname.
So, I used document.getElementByTagName("table")[2];
Later, I tried to obtain value of an element in that table by table.children[0].children[1].children[2].innerHTML;
Then, I tried to modify the already existing <p>
element with id="ID"
.
But I'm not getting the value modified?
Whats wrong with my script?
<!DOCTYPE HTML>
<html>
<body>
ID : <p id="ID"></p>
<body>
<table>
</table>
<table>
</table>
<table>
<tbody>
<tr>
</tr>
<tr>
<td>Name</td>
<td>Class</td>
<td>25</td>
</tr>
<tr>
</tr>
</tbody>
</table>
<script>
var table = document.getElementByTagName("table")[2];
var id = table.children[0].children[1].children[2].innerHTML;
document.getElementById("ID").innerHTML = id;
</script>
</body>
</html>
As with most DOM methods that return a Node list, the name is plural - getElementsByTagName
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With