document queryselector picks first or last

Solutions on MaxInterview for document queryselector picks first or last by the best coders in the world

we are a community of more than 2 million smartest coders
registration for
employee referral programs
are now open
get referred to google, amazon, flipkart and more
register now
  
showing results for - "document queryselector picks first or last"
Aymen
29 Jun 2020
1var elFirst = document.querySelector(".myclass"); //first element
2var elLast = document.querySelector(".myclass:last-child"); //last element
3
4But be careful!
5:last-child and :first-child are relevant to the parent element, 
6not to the list! It means that there could be several first-childs 
7and several last-childs