Answers for "javascript get element nth child"

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
1

javascript get element nth child

//you can use the css nth-child property like this:
var second-child = document.querySeletorAll('[your element name]:nth-child(2)');
Posted by: Guest on December-26-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language