Let's look at an example
<ul > <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ul>
To get the last child of a list, you can simply use queryselector
document.querySelector('li:last-child'); //this will return Milk which is the last child of the list document.querySelector('li:first-child') OR document.querySelector('li'); //both will give you the first child of the list
Suppose you need the nth child element for which you can use the following syntax:
document.querySelector('li:nth-child(2)');
If you want all the list items in this list:
document.getElementsByTagName('li') //(Will return the whole list)here i am using tagname. It can be also applied on classname or element id
source share