Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the nth-child number of an element in jquery

Tags:

html

jquery

I have a class of multiple 'DIV' elements and inside it are list of 'p' elements. See below:

<div class="container">     <p>This is content 1</p>     <p>This is content 2</p>     <p>This is content 3</p> </div> <div class="container">     <p>This is content 1</p>     <p>This is content 2</p>     <p>This is content 3</p> </div> 

Here's my jQuery code on calling the 'p' elements through hover:

$('.container').children('p').hover(function(){     //get the nth child of p from parent class 'container' }); 

How can I get the nth child number of the element 'p' from its parent container class 'container'?

Like if you hover

This is content 1

it should trigger output as 1;

like image 888
PHP Noob Avatar asked May 11 '12 07:05

PHP Noob


People also ask

How do you find the nth child of an element?

Use the querySelector() method to get the nth child of an element, e.g. document. querySelector('#parent :nth-child(3)') . The :nth-child pseudo class returns the element that matches the provided position.

What is nth child in jQuery?

Definition and Usage. The :nth-child(n) selector selects all elements that are the nth child, regardless of type, of their parent. Tip: Use the :nth-of-type() selector to select all elements that are the nth child, of a particular type, of their parent.

How can I get second child in jQuery?

Using jQuery :nth-child Selector You have put the position of an element as its argument which is 2 as you want to select the second li element. If you want to get the exact element, you have to specify the index value of the item. A list element starts with an index 0.

How do you use the nth child in querySelector?

To get the nth-child of an element using the querySelector method, pass the :nth-child() pseudo-class as a parameter to the method, e.g. document. querySelector('#parent :nth-child(1)') . The nth-child pseudo-class returns the element that matches the specified position.


2 Answers

You can use jQuery's index function for that. It tells you where the given element is relative to its siblings:

var index = $(this).index(); 

Live example | source

The indexes are 0-based, so if you're looking for a 1-based index (e.g., where the first one is 1 rather than 0), just add one to it:

var index = $(this).index() + 1; 

If you're not using jQuery and came across this question and answer (the OP was using jQuery), this is also quite simple to do without it. nth-child only considers elements, so:

function findChildIndex(node) {     var index = 1;                         // nth-child starts with 1 = first child     // (You could argue that you should throw an exception here if the     // `node` passed in is not an element [e.g., is a text node etc.]     // or null.)     while (node.previousSibling) {         node = node.previousSibling;         if (node && node.nodeType === 1) { // 1 = element             ++index;         }     }     return index; } 
like image 115
T.J. Crowder Avatar answered Oct 03 '22 18:10

T.J. Crowder


Use the parameter-less version of the .index() method to find the position of the element relative to its siblings:

$('.container').children('p').hover(function() {      var index = $(this).index() + 1; }); 

Note that the result of .index() will be zero-based, not one-based, hence the + 1

like image 41
Alnitak Avatar answered Oct 03 '22 18:10

Alnitak