I have two tables like this:
<table>
<tr>
<td></td>
<td></td>
</tr>
</table>
<table>
<tr>
<td></td>
<td></td>
</tr>
</table>
They are all same. I have to select the first one (and the second) using pure javascript.
In jQuery it is $(table:first).
If tables were like this (with classes) could I use getElementByClassName('class')[0]
<table class="class">
<tr>
<td></td>
<td></td>
</tr>
</table>
<table class="class">
<tr>
<td></td>
<td></td>
</tr>
</table>
You could use
.getElementsByTagName("table")on thedocument, which will return aNodeListcontaining all tables within the document. NodeLists are array-like objects and the tables are returned in the same order as they have in the document, so you could then just take the first element using its index.NodeLists are live
It is worth noting that the
NodeListreturned by.getElementsByTagName()is live, meaning that if you do DOM-manipulations after your call to.getElementsByTagName(), those manipulations will be reflected in your list.