Answers for "how to get the nth child of dom js"

3

how to get the nth child of dom js

//method one 
  var c = document.getElementById("myDIV").childNodes;
  c[1].style.backgroundColor = "yellow";//first child
  c[2].style.backgroundColor = "red";//second child
  c[-1].style.backgroundColor = "green";//last child
  c[-2].style.backgroundColor = "blue";//secound last child
//method two
 let nth-child = document.querySeletorAll('myDiv:nth-child(n)');//gives nth child
Posted by: Guest on April-28-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language