-4

Which is better or what are the differences, when you want to select an nth child? Please note I do not want to use jQuery.

  • 4
    You're not very good at searching: http://stackoverflow.com/questions/12048273/selecting-second-children-of-first-div-children-in-javascript, http://www.codingforums.com/dom-json-scripting/191794-how-get-second-child-dom.html – Charlie Mar 09 '14 at 22:51
  • what if it has different tags? – pwolaq Mar 09 '14 at 22:53
  • 2
    `element.children[1]` – Felix Kling Mar 09 '14 at 22:53
  • Question should be accompanied by [**your code**](http://whathaveyoutried.com) you are having issues with and the relevant HTML mark-up and if possible include a [**jsfiddle**](http://jsfiddle.net), or similar, demonstrating the issue. In addition get to know some good resources and the basics for the language you are working with. The [**W3.org on scripts**](http://www.w3.org/standards/webdesign/script) and the [**MDN on JavaScript**](https://developer.mozilla.org/en/docs/Web/JavaScript) are a good start. – Nope Mar 09 '14 at 23:16

1 Answers1

6

Well, you don't provide much information, but you've got two ways:

Let node be a DOM node:

node.childNodes[1]; //0 is the first child.

Another way...

node.querySelector(':nth-child(2)'); //in selectors, the first child has index 1.
Oscar Paz
  • 18,084
  • 3
  • 27
  • 42