Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery difference between :eq() and :nth-child()

In jQuery, what are some of the key differences between using :eq() and :nth-child() to select any elements ?

Also in general, for the starting index, in which case does it start from "0" and when it starts from "1" ?

like image 589
copenndthagen Avatar asked Aug 12 '11 12:08

copenndthagen


People also ask

What is the difference between eq () and get () methods in jQuery?

eq() returns it as a jQuery object, meaning the DOM element is wrapped in the jQuery wrapper, which means that it accepts jQuery functions. . get() returns an array of raw DOM elements. You may manipulate each of them by accessing its attributes and invoking its functions as you would on a raw DOM element.

What is the difference between the nth child () and Nth of type () selector?

As a general rule, if you want to select an interval of a selector regardless of the type of element it is, use nth-child . However, if you want to select a specific type only and apply an interval selection from there, use nth-of-type .

What is the use of eq in jQuery?

Definition and Usage The :eq() selector selects an element with a specific index number. The index numbers start at 0, so the first element will have the index number 0 (not 1). This is mostly used together with another selector to select a specifically indexed element in a group (like in the example above).

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.


2 Answers

:eq()

Select the element at index n within the matched set.

The index-related selectors (:eq(), :lt(), :gt(), :even, :odd) filter the set of elements that have matched the expressions that precede them. They narrow the set down based on the order of the elements within this matched set. For example, if elements are first selected with a class selector (.myclass) and four elements are returned, these elements are given indices 0 through 3 for the purposes of these selectors.

:nth-child()

Selects all elements that are the nth-child of their parent.

Because jQuery's implementation of :nth-child(n) is strictly derived from the CSS specification, the value of n is "1-indexed", meaning that the counting starts at 1. For all other selector expressions, however, jQuery follows JavaScript's "0-indexed" counting. Therefore, given a single containing two <li>s, $('li:nth-child(1)') selects the first <li> while $('li:eq(1)') selects the second.

The :nth-child(n) pseudo-class is easily confused with :eq(n), even though the two can result in dramatically different matched elements. With :nth-child(n), all children are counted, regardless of what they are, and the specified element is selected only if it matches the selector attached to the pseudo-class. With :eq(n) only the selector attached to the pseudo-class is counted, not limited to children of any other element, and the (n+1)th one (n is 0-based) is selected.

The :nth-child(an+b) pseudo-class notation represents an element that has an+b-1 siblings before it in the document tree, for any positive integer or zero value of n, and has a parent element. For values of a and b greater than zero, this effectively divides the element's children into groups of a elements (the last group taking the remainder), and selecting the bth element of each group. For example, this allows the selectors to address every other row in a table, and could be used to alternate the color of paragraph text in a cycle of four. The a and b values must be integers (positive, negative, or zero). The index of the first child of an element is 1.

In addition to this, :nth-child() can take ‘odd’ and ‘even’ as arguments instead. ‘odd’ has the same signification as 2n+1, and ‘even’ has the same signification as 2n.

Further discussion of this unusual usage can be found in the W3C CSS specification.

Detailed Comparision

See the Demo: http://jsfiddle.net/rathoreahsan/sXHtB/ -- Link updated

Also See the references

http://api.jquery.com/eq-selector/

http://api.jquery.com/nth-child-selector/

like image 164
Ahsan Rathod Avatar answered Sep 21 '22 06:09

Ahsan Rathod


:nth-child() Selector: selects all elements that are the nth-child of their parent.

:eq() Selector: Select the element at index n within the matched set.

See: http://api.jquery.com/eq-selector/ and http://api.jquery.com/nth-child-selector/

Good luck.

like image 32
defuz Avatar answered Sep 18 '22 06:09

defuz