Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getElementByTagName() not working? [closed]

  1. 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];

  2. Later, I tried to obtain value of an element in that table by table.children[0].children[1].children[2].innerHTML;

  3. 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>
like image 886
Nikhil Avatar asked Mar 26 '14 12:03

Nikhil


1 Answers

As with most DOM methods that return a Node list, the name is plural - getElementsByTagName.

like image 156
Quentin Avatar answered Sep 29 '22 07:09

Quentin