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











up vote
30
down vote

favorite
12












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" ?










share|improve this question


























    up vote
    30
    down vote

    favorite
    12












    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" ?










    share|improve this question
























      up vote
      30
      down vote

      favorite
      12









      up vote
      30
      down vote

      favorite
      12






      12





      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" ?










      share|improve this question













      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" ?







      javascript jquery dom jquery-selectors






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 12 '11 at 12:20









      testndtv

      18.6k74240370




      18.6k74240370
























          8 Answers
          8






          active

          oldest

          votes

















          up vote
          54
          down vote



          accepted










          :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/






          share|improve this answer























          • You seem to have hit the nail on the head...The info in the 2nd para (where you say :nth-child(n) pseudo-class is easily confused with :eq(n),) is what I am looking for....Even though it is a bit tough to understand, will it be possible for you to update the fiddle demonstating this difference..I have already read the jQuery api doc, but still could not understand it very clearly... But SURELY...A++++ to you for UNDERSTANDING the question SO VERY CLEARLY...
            – testndtv
            Aug 12 '11 at 13:26








          • 2




            Answer Updated, new demo added
            – Ahsan Rathod
            Aug 12 '11 at 14:07










          • Thx again..in your example, i am not very clear why only odd elements are selected by $('.message1 p:nth-child(2n+2)').css({"color" : "red"});
            – testndtv
            Aug 13 '11 at 6:30










          • You can select even as well...
            – Ahsan Rathod
            Aug 13 '11 at 9:25










          • Thx..actually my question is what is that n exactly..does the parameter in () represent just 1 value or is it a set?
            – testndtv
            Aug 16 '11 at 10:55


















          up vote
          13
          down vote














          :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.






          share|improve this answer






























            up vote
            7
            down vote













            :eq() allows you to access the elements in the jQuery object by index



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



            :nth-child also allows you to access the an element by index, however it only applies to the term to the immediate left of it.



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



            This means that if you want to pick one element from a selector then use :eq if you want to restrict selections to elements with n-1 preceding-sibilings then use nth-child.



            Javascript arrays are usually indexed from 0; however nth-child is indexed from 1 because it is technically a CSS property as opposed to a jQuery one.






            share|improve this answer






























              up vote
              0
              down vote













              eq() starts with 0, while nth-child() starts with 1



              see differences clearly here
              http://jsfiddle.net/9xu2R/






              share|improve this answer

















              • 1




                -1 The start index is not the only difference between eq and nth-child. I've expanded your demo with more detail jsfiddle.net/9xu2R/18
                – Walter Stabosz
                Jun 30 '13 at 15:10


















              up vote
              0
              down vote













              $("#dataTable tr:nth-child(1) td:nth-child(2)").html();


              here dataTable is a table



              <table id="dataTable" width="50%">
              <thead>
              <th>Name</th>
              <th>EnrollNo.</th>
              </thead>
              <tbody>
              <tr>
              <td>Somdip</td><td>IT001<td>
              </tr>
              <tr>
              <td>Sanyal</td><td>IT002<td>
              </tr>
              </tbody>
              </table>


              The nth-child selector of jquery will help you to fetch the exact cell values from this table. A practical example where tr:nth-child(1) td:nth-child(2) fetches the 1,2 cell of the table.






              share|improve this answer






























                up vote
                0
                down vote













                nth-child selects the nth child of parent object(s) other selects n-th element in a collection (index starting from 0 or 1 is only a trivial part the difference).
                so saying tr td:nth-child(5) selects every tr and gets their 5th children where as eq gets all tds in all trs and selects only 5th td ... The main difference is that. Indeed the wording of the documentation does not point out that fact straight but garbles the words like it is black magic ...






                share|improve this answer




























                  up vote
                  0
                  down vote













                  CSS :first-child, :nth-child, and :last-child are not like :eq



                  So if we had a snippet of HTML like



                  <div>
                  <div id="bar1" class="foo"></div>
                  <div id="bar2" class="foo"></div>
                  <div id="bar3" class="foo"></div>
                  </div>


                  Then the selector .foo:nth-child(2)will match the div #bar2. If we insert another element at the front of the container:



                  <div>
                  <p>Shift!</p>
                  <div id="bar1" class="foo"></div>
                  <div id="bar2" class="foo"></div>
                  <div id="bar3" class="foo"></div>
                  </div>


                  And again we select .foo:nth-child(2), we match the div #bar1 because the 2nd child of the container also matches .foo.



                  Thus, in this second example, if we try .foo:nth-child(1) or the equivalent .foo:first-child, we will not match any elements because the first child element in that container — the p tag — does not match .foo.



                  Likewise, :nth-child can match children in multiple containers. In the HTML snippet:



                  <div>
                  <p>Shift!</p>
                  <div id="bar1" class="foo"></div>
                  <div id="bar2" class="foo"></div>
                  <div id="bar3" class="foo"></div>
                  </div>

                  <div>
                  <div id="quux" class="foo"></div>
                  </div>


                  the selector .foo:last-child will match the divs #bar3 and #quux; but .foo:first-child or .foo:nth-child(1) will only match #quux because the first child of the first container is, again, not a .foo.



                  Source https://content.pivotal.io/blog/css-first-child-nth-child-and-last-child-are-not-like-eq






                  share|improve this answer




























                    up vote
                    -2
                    down vote













                    :eq is array indexed based, so it starts from 0. It is also not part of the Css specification.



                    Whereas :nth-child is part of the Css specification and refers to the element position in a DOM tree.



                    In terms of usage, they both do the same thing.



                    Demo here






                    share|improve this answer























                      Your Answer






                      StackExchange.ifUsing("editor", function () {
                      StackExchange.using("externalEditor", function () {
                      StackExchange.using("snippets", function () {
                      StackExchange.snippets.init();
                      });
                      });
                      }, "code-snippets");

                      StackExchange.ready(function() {
                      var channelOptions = {
                      tags: "".split(" "),
                      id: "1"
                      };
                      initTagRenderer("".split(" "), "".split(" "), channelOptions);

                      StackExchange.using("externalEditor", function() {
                      // Have to fire editor after snippets, if snippets enabled
                      if (StackExchange.settings.snippets.snippetsEnabled) {
                      StackExchange.using("snippets", function() {
                      createEditor();
                      });
                      }
                      else {
                      createEditor();
                      }
                      });

                      function createEditor() {
                      StackExchange.prepareEditor({
                      heartbeatType: 'answer',
                      convertImagesToLinks: true,
                      noModals: true,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: 10,
                      bindNavPrevention: true,
                      postfix: "",
                      imageUploader: {
                      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                      allowUrls: true
                      },
                      onDemand: true,
                      discardSelector: ".discard-answer"
                      ,immediatelyShowMarkdownHelp:true
                      });


                      }
                      });














                      draft saved

                      draft discarded


















                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7039966%2fjquery-difference-between-eq-and-nth-child%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown

























                      8 Answers
                      8






                      active

                      oldest

                      votes








                      8 Answers
                      8






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes








                      up vote
                      54
                      down vote



                      accepted










                      :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/






                      share|improve this answer























                      • You seem to have hit the nail on the head...The info in the 2nd para (where you say :nth-child(n) pseudo-class is easily confused with :eq(n),) is what I am looking for....Even though it is a bit tough to understand, will it be possible for you to update the fiddle demonstating this difference..I have already read the jQuery api doc, but still could not understand it very clearly... But SURELY...A++++ to you for UNDERSTANDING the question SO VERY CLEARLY...
                        – testndtv
                        Aug 12 '11 at 13:26








                      • 2




                        Answer Updated, new demo added
                        – Ahsan Rathod
                        Aug 12 '11 at 14:07










                      • Thx again..in your example, i am not very clear why only odd elements are selected by $('.message1 p:nth-child(2n+2)').css({"color" : "red"});
                        – testndtv
                        Aug 13 '11 at 6:30










                      • You can select even as well...
                        – Ahsan Rathod
                        Aug 13 '11 at 9:25










                      • Thx..actually my question is what is that n exactly..does the parameter in () represent just 1 value or is it a set?
                        – testndtv
                        Aug 16 '11 at 10:55















                      up vote
                      54
                      down vote



                      accepted










                      :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/






                      share|improve this answer























                      • You seem to have hit the nail on the head...The info in the 2nd para (where you say :nth-child(n) pseudo-class is easily confused with :eq(n),) is what I am looking for....Even though it is a bit tough to understand, will it be possible for you to update the fiddle demonstating this difference..I have already read the jQuery api doc, but still could not understand it very clearly... But SURELY...A++++ to you for UNDERSTANDING the question SO VERY CLEARLY...
                        – testndtv
                        Aug 12 '11 at 13:26








                      • 2




                        Answer Updated, new demo added
                        – Ahsan Rathod
                        Aug 12 '11 at 14:07










                      • Thx again..in your example, i am not very clear why only odd elements are selected by $('.message1 p:nth-child(2n+2)').css({"color" : "red"});
                        – testndtv
                        Aug 13 '11 at 6:30










                      • You can select even as well...
                        – Ahsan Rathod
                        Aug 13 '11 at 9:25










                      • Thx..actually my question is what is that n exactly..does the parameter in () represent just 1 value or is it a set?
                        – testndtv
                        Aug 16 '11 at 10:55













                      up vote
                      54
                      down vote



                      accepted







                      up vote
                      54
                      down vote



                      accepted






                      :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/






                      share|improve this answer














                      :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/







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Feb 22 '13 at 9:55









                      BoltClock

                      512k12711481190




                      512k12711481190










                      answered Aug 12 '11 at 12:24









                      Ahsan Rathod

                      4,77121624




                      4,77121624












                      • You seem to have hit the nail on the head...The info in the 2nd para (where you say :nth-child(n) pseudo-class is easily confused with :eq(n),) is what I am looking for....Even though it is a bit tough to understand, will it be possible for you to update the fiddle demonstating this difference..I have already read the jQuery api doc, but still could not understand it very clearly... But SURELY...A++++ to you for UNDERSTANDING the question SO VERY CLEARLY...
                        – testndtv
                        Aug 12 '11 at 13:26








                      • 2




                        Answer Updated, new demo added
                        – Ahsan Rathod
                        Aug 12 '11 at 14:07










                      • Thx again..in your example, i am not very clear why only odd elements are selected by $('.message1 p:nth-child(2n+2)').css({"color" : "red"});
                        – testndtv
                        Aug 13 '11 at 6:30










                      • You can select even as well...
                        – Ahsan Rathod
                        Aug 13 '11 at 9:25










                      • Thx..actually my question is what is that n exactly..does the parameter in () represent just 1 value or is it a set?
                        – testndtv
                        Aug 16 '11 at 10:55


















                      • You seem to have hit the nail on the head...The info in the 2nd para (where you say :nth-child(n) pseudo-class is easily confused with :eq(n),) is what I am looking for....Even though it is a bit tough to understand, will it be possible for you to update the fiddle demonstating this difference..I have already read the jQuery api doc, but still could not understand it very clearly... But SURELY...A++++ to you for UNDERSTANDING the question SO VERY CLEARLY...
                        – testndtv
                        Aug 12 '11 at 13:26








                      • 2




                        Answer Updated, new demo added
                        – Ahsan Rathod
                        Aug 12 '11 at 14:07










                      • Thx again..in your example, i am not very clear why only odd elements are selected by $('.message1 p:nth-child(2n+2)').css({"color" : "red"});
                        – testndtv
                        Aug 13 '11 at 6:30










                      • You can select even as well...
                        – Ahsan Rathod
                        Aug 13 '11 at 9:25










                      • Thx..actually my question is what is that n exactly..does the parameter in () represent just 1 value or is it a set?
                        – testndtv
                        Aug 16 '11 at 10:55
















                      You seem to have hit the nail on the head...The info in the 2nd para (where you say :nth-child(n) pseudo-class is easily confused with :eq(n),) is what I am looking for....Even though it is a bit tough to understand, will it be possible for you to update the fiddle demonstating this difference..I have already read the jQuery api doc, but still could not understand it very clearly... But SURELY...A++++ to you for UNDERSTANDING the question SO VERY CLEARLY...
                      – testndtv
                      Aug 12 '11 at 13:26






                      You seem to have hit the nail on the head...The info in the 2nd para (where you say :nth-child(n) pseudo-class is easily confused with :eq(n),) is what I am looking for....Even though it is a bit tough to understand, will it be possible for you to update the fiddle demonstating this difference..I have already read the jQuery api doc, but still could not understand it very clearly... But SURELY...A++++ to you for UNDERSTANDING the question SO VERY CLEARLY...
                      – testndtv
                      Aug 12 '11 at 13:26






                      2




                      2




                      Answer Updated, new demo added
                      – Ahsan Rathod
                      Aug 12 '11 at 14:07




                      Answer Updated, new demo added
                      – Ahsan Rathod
                      Aug 12 '11 at 14:07












                      Thx again..in your example, i am not very clear why only odd elements are selected by $('.message1 p:nth-child(2n+2)').css({"color" : "red"});
                      – testndtv
                      Aug 13 '11 at 6:30




                      Thx again..in your example, i am not very clear why only odd elements are selected by $('.message1 p:nth-child(2n+2)').css({"color" : "red"});
                      – testndtv
                      Aug 13 '11 at 6:30












                      You can select even as well...
                      – Ahsan Rathod
                      Aug 13 '11 at 9:25




                      You can select even as well...
                      – Ahsan Rathod
                      Aug 13 '11 at 9:25












                      Thx..actually my question is what is that n exactly..does the parameter in () represent just 1 value or is it a set?
                      – testndtv
                      Aug 16 '11 at 10:55




                      Thx..actually my question is what is that n exactly..does the parameter in () represent just 1 value or is it a set?
                      – testndtv
                      Aug 16 '11 at 10:55












                      up vote
                      13
                      down vote














                      :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.






                      share|improve this answer



























                        up vote
                        13
                        down vote














                        :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.






                        share|improve this answer

























                          up vote
                          13
                          down vote










                          up vote
                          13
                          down vote










                          :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.






                          share|improve this answer















                          :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.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Aug 12 '11 at 12:31









                          cHao

                          67.7k13112151




                          67.7k13112151










                          answered Aug 12 '11 at 12:23









                          defuz

                          16k72952




                          16k72952






















                              up vote
                              7
                              down vote













                              :eq() allows you to access the elements in the jQuery object by index



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



                              :nth-child also allows you to access the an element by index, however it only applies to the term to the immediate left of it.



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



                              This means that if you want to pick one element from a selector then use :eq if you want to restrict selections to elements with n-1 preceding-sibilings then use nth-child.



                              Javascript arrays are usually indexed from 0; however nth-child is indexed from 1 because it is technically a CSS property as opposed to a jQuery one.






                              share|improve this answer



























                                up vote
                                7
                                down vote













                                :eq() allows you to access the elements in the jQuery object by index



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



                                :nth-child also allows you to access the an element by index, however it only applies to the term to the immediate left of it.



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



                                This means that if you want to pick one element from a selector then use :eq if you want to restrict selections to elements with n-1 preceding-sibilings then use nth-child.



                                Javascript arrays are usually indexed from 0; however nth-child is indexed from 1 because it is technically a CSS property as opposed to a jQuery one.






                                share|improve this answer

























                                  up vote
                                  7
                                  down vote










                                  up vote
                                  7
                                  down vote









                                  :eq() allows you to access the elements in the jQuery object by index



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



                                  :nth-child also allows you to access the an element by index, however it only applies to the term to the immediate left of it.



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



                                  This means that if you want to pick one element from a selector then use :eq if you want to restrict selections to elements with n-1 preceding-sibilings then use nth-child.



                                  Javascript arrays are usually indexed from 0; however nth-child is indexed from 1 because it is technically a CSS property as opposed to a jQuery one.






                                  share|improve this answer














                                  :eq() allows you to access the elements in the jQuery object by index



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



                                  :nth-child also allows you to access the an element by index, however it only applies to the term to the immediate left of it.



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



                                  This means that if you want to pick one element from a selector then use :eq if you want to restrict selections to elements with n-1 preceding-sibilings then use nth-child.



                                  Javascript arrays are usually indexed from 0; however nth-child is indexed from 1 because it is technically a CSS property as opposed to a jQuery one.







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Dec 16 '11 at 18:08







                                  user212218

















                                  answered Aug 12 '11 at 12:24









                                  MikeBaker

                                  50244




                                  50244






















                                      up vote
                                      0
                                      down vote













                                      eq() starts with 0, while nth-child() starts with 1



                                      see differences clearly here
                                      http://jsfiddle.net/9xu2R/






                                      share|improve this answer

















                                      • 1




                                        -1 The start index is not the only difference between eq and nth-child. I've expanded your demo with more detail jsfiddle.net/9xu2R/18
                                        – Walter Stabosz
                                        Jun 30 '13 at 15:10















                                      up vote
                                      0
                                      down vote













                                      eq() starts with 0, while nth-child() starts with 1



                                      see differences clearly here
                                      http://jsfiddle.net/9xu2R/






                                      share|improve this answer

















                                      • 1




                                        -1 The start index is not the only difference between eq and nth-child. I've expanded your demo with more detail jsfiddle.net/9xu2R/18
                                        – Walter Stabosz
                                        Jun 30 '13 at 15:10













                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      eq() starts with 0, while nth-child() starts with 1



                                      see differences clearly here
                                      http://jsfiddle.net/9xu2R/






                                      share|improve this answer












                                      eq() starts with 0, while nth-child() starts with 1



                                      see differences clearly here
                                      http://jsfiddle.net/9xu2R/







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Aug 12 '11 at 12:28









                                      JSantos

                                      1,5801832




                                      1,5801832








                                      • 1




                                        -1 The start index is not the only difference between eq and nth-child. I've expanded your demo with more detail jsfiddle.net/9xu2R/18
                                        – Walter Stabosz
                                        Jun 30 '13 at 15:10














                                      • 1




                                        -1 The start index is not the only difference between eq and nth-child. I've expanded your demo with more detail jsfiddle.net/9xu2R/18
                                        – Walter Stabosz
                                        Jun 30 '13 at 15:10








                                      1




                                      1




                                      -1 The start index is not the only difference between eq and nth-child. I've expanded your demo with more detail jsfiddle.net/9xu2R/18
                                      – Walter Stabosz
                                      Jun 30 '13 at 15:10




                                      -1 The start index is not the only difference between eq and nth-child. I've expanded your demo with more detail jsfiddle.net/9xu2R/18
                                      – Walter Stabosz
                                      Jun 30 '13 at 15:10










                                      up vote
                                      0
                                      down vote













                                      $("#dataTable tr:nth-child(1) td:nth-child(2)").html();


                                      here dataTable is a table



                                      <table id="dataTable" width="50%">
                                      <thead>
                                      <th>Name</th>
                                      <th>EnrollNo.</th>
                                      </thead>
                                      <tbody>
                                      <tr>
                                      <td>Somdip</td><td>IT001<td>
                                      </tr>
                                      <tr>
                                      <td>Sanyal</td><td>IT002<td>
                                      </tr>
                                      </tbody>
                                      </table>


                                      The nth-child selector of jquery will help you to fetch the exact cell values from this table. A practical example where tr:nth-child(1) td:nth-child(2) fetches the 1,2 cell of the table.






                                      share|improve this answer



























                                        up vote
                                        0
                                        down vote













                                        $("#dataTable tr:nth-child(1) td:nth-child(2)").html();


                                        here dataTable is a table



                                        <table id="dataTable" width="50%">
                                        <thead>
                                        <th>Name</th>
                                        <th>EnrollNo.</th>
                                        </thead>
                                        <tbody>
                                        <tr>
                                        <td>Somdip</td><td>IT001<td>
                                        </tr>
                                        <tr>
                                        <td>Sanyal</td><td>IT002<td>
                                        </tr>
                                        </tbody>
                                        </table>


                                        The nth-child selector of jquery will help you to fetch the exact cell values from this table. A practical example where tr:nth-child(1) td:nth-child(2) fetches the 1,2 cell of the table.






                                        share|improve this answer

























                                          up vote
                                          0
                                          down vote










                                          up vote
                                          0
                                          down vote









                                          $("#dataTable tr:nth-child(1) td:nth-child(2)").html();


                                          here dataTable is a table



                                          <table id="dataTable" width="50%">
                                          <thead>
                                          <th>Name</th>
                                          <th>EnrollNo.</th>
                                          </thead>
                                          <tbody>
                                          <tr>
                                          <td>Somdip</td><td>IT001<td>
                                          </tr>
                                          <tr>
                                          <td>Sanyal</td><td>IT002<td>
                                          </tr>
                                          </tbody>
                                          </table>


                                          The nth-child selector of jquery will help you to fetch the exact cell values from this table. A practical example where tr:nth-child(1) td:nth-child(2) fetches the 1,2 cell of the table.






                                          share|improve this answer














                                          $("#dataTable tr:nth-child(1) td:nth-child(2)").html();


                                          here dataTable is a table



                                          <table id="dataTable" width="50%">
                                          <thead>
                                          <th>Name</th>
                                          <th>EnrollNo.</th>
                                          </thead>
                                          <tbody>
                                          <tr>
                                          <td>Somdip</td><td>IT001<td>
                                          </tr>
                                          <tr>
                                          <td>Sanyal</td><td>IT002<td>
                                          </tr>
                                          </tbody>
                                          </table>


                                          The nth-child selector of jquery will help you to fetch the exact cell values from this table. A practical example where tr:nth-child(1) td:nth-child(2) fetches the 1,2 cell of the table.







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Feb 19 '13 at 12:58









                                          Barna Tekse

                                          2,40272335




                                          2,40272335










                                          answered Feb 19 '13 at 12:52









                                          somdip

                                          12




                                          12






















                                              up vote
                                              0
                                              down vote













                                              nth-child selects the nth child of parent object(s) other selects n-th element in a collection (index starting from 0 or 1 is only a trivial part the difference).
                                              so saying tr td:nth-child(5) selects every tr and gets their 5th children where as eq gets all tds in all trs and selects only 5th td ... The main difference is that. Indeed the wording of the documentation does not point out that fact straight but garbles the words like it is black magic ...






                                              share|improve this answer

























                                                up vote
                                                0
                                                down vote













                                                nth-child selects the nth child of parent object(s) other selects n-th element in a collection (index starting from 0 or 1 is only a trivial part the difference).
                                                so saying tr td:nth-child(5) selects every tr and gets their 5th children where as eq gets all tds in all trs and selects only 5th td ... The main difference is that. Indeed the wording of the documentation does not point out that fact straight but garbles the words like it is black magic ...






                                                share|improve this answer























                                                  up vote
                                                  0
                                                  down vote










                                                  up vote
                                                  0
                                                  down vote









                                                  nth-child selects the nth child of parent object(s) other selects n-th element in a collection (index starting from 0 or 1 is only a trivial part the difference).
                                                  so saying tr td:nth-child(5) selects every tr and gets their 5th children where as eq gets all tds in all trs and selects only 5th td ... The main difference is that. Indeed the wording of the documentation does not point out that fact straight but garbles the words like it is black magic ...






                                                  share|improve this answer












                                                  nth-child selects the nth child of parent object(s) other selects n-th element in a collection (index starting from 0 or 1 is only a trivial part the difference).
                                                  so saying tr td:nth-child(5) selects every tr and gets their 5th children where as eq gets all tds in all trs and selects only 5th td ... The main difference is that. Indeed the wording of the documentation does not point out that fact straight but garbles the words like it is black magic ...







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Nov 21 '13 at 11:37









                                                  karadeniz

                                                  1




                                                  1






















                                                      up vote
                                                      0
                                                      down vote













                                                      CSS :first-child, :nth-child, and :last-child are not like :eq



                                                      So if we had a snippet of HTML like



                                                      <div>
                                                      <div id="bar1" class="foo"></div>
                                                      <div id="bar2" class="foo"></div>
                                                      <div id="bar3" class="foo"></div>
                                                      </div>


                                                      Then the selector .foo:nth-child(2)will match the div #bar2. If we insert another element at the front of the container:



                                                      <div>
                                                      <p>Shift!</p>
                                                      <div id="bar1" class="foo"></div>
                                                      <div id="bar2" class="foo"></div>
                                                      <div id="bar3" class="foo"></div>
                                                      </div>


                                                      And again we select .foo:nth-child(2), we match the div #bar1 because the 2nd child of the container also matches .foo.



                                                      Thus, in this second example, if we try .foo:nth-child(1) or the equivalent .foo:first-child, we will not match any elements because the first child element in that container — the p tag — does not match .foo.



                                                      Likewise, :nth-child can match children in multiple containers. In the HTML snippet:



                                                      <div>
                                                      <p>Shift!</p>
                                                      <div id="bar1" class="foo"></div>
                                                      <div id="bar2" class="foo"></div>
                                                      <div id="bar3" class="foo"></div>
                                                      </div>

                                                      <div>
                                                      <div id="quux" class="foo"></div>
                                                      </div>


                                                      the selector .foo:last-child will match the divs #bar3 and #quux; but .foo:first-child or .foo:nth-child(1) will only match #quux because the first child of the first container is, again, not a .foo.



                                                      Source https://content.pivotal.io/blog/css-first-child-nth-child-and-last-child-are-not-like-eq






                                                      share|improve this answer

























                                                        up vote
                                                        0
                                                        down vote













                                                        CSS :first-child, :nth-child, and :last-child are not like :eq



                                                        So if we had a snippet of HTML like



                                                        <div>
                                                        <div id="bar1" class="foo"></div>
                                                        <div id="bar2" class="foo"></div>
                                                        <div id="bar3" class="foo"></div>
                                                        </div>


                                                        Then the selector .foo:nth-child(2)will match the div #bar2. If we insert another element at the front of the container:



                                                        <div>
                                                        <p>Shift!</p>
                                                        <div id="bar1" class="foo"></div>
                                                        <div id="bar2" class="foo"></div>
                                                        <div id="bar3" class="foo"></div>
                                                        </div>


                                                        And again we select .foo:nth-child(2), we match the div #bar1 because the 2nd child of the container also matches .foo.



                                                        Thus, in this second example, if we try .foo:nth-child(1) or the equivalent .foo:first-child, we will not match any elements because the first child element in that container — the p tag — does not match .foo.



                                                        Likewise, :nth-child can match children in multiple containers. In the HTML snippet:



                                                        <div>
                                                        <p>Shift!</p>
                                                        <div id="bar1" class="foo"></div>
                                                        <div id="bar2" class="foo"></div>
                                                        <div id="bar3" class="foo"></div>
                                                        </div>

                                                        <div>
                                                        <div id="quux" class="foo"></div>
                                                        </div>


                                                        the selector .foo:last-child will match the divs #bar3 and #quux; but .foo:first-child or .foo:nth-child(1) will only match #quux because the first child of the first container is, again, not a .foo.



                                                        Source https://content.pivotal.io/blog/css-first-child-nth-child-and-last-child-are-not-like-eq






                                                        share|improve this answer























                                                          up vote
                                                          0
                                                          down vote










                                                          up vote
                                                          0
                                                          down vote









                                                          CSS :first-child, :nth-child, and :last-child are not like :eq



                                                          So if we had a snippet of HTML like



                                                          <div>
                                                          <div id="bar1" class="foo"></div>
                                                          <div id="bar2" class="foo"></div>
                                                          <div id="bar3" class="foo"></div>
                                                          </div>


                                                          Then the selector .foo:nth-child(2)will match the div #bar2. If we insert another element at the front of the container:



                                                          <div>
                                                          <p>Shift!</p>
                                                          <div id="bar1" class="foo"></div>
                                                          <div id="bar2" class="foo"></div>
                                                          <div id="bar3" class="foo"></div>
                                                          </div>


                                                          And again we select .foo:nth-child(2), we match the div #bar1 because the 2nd child of the container also matches .foo.



                                                          Thus, in this second example, if we try .foo:nth-child(1) or the equivalent .foo:first-child, we will not match any elements because the first child element in that container — the p tag — does not match .foo.



                                                          Likewise, :nth-child can match children in multiple containers. In the HTML snippet:



                                                          <div>
                                                          <p>Shift!</p>
                                                          <div id="bar1" class="foo"></div>
                                                          <div id="bar2" class="foo"></div>
                                                          <div id="bar3" class="foo"></div>
                                                          </div>

                                                          <div>
                                                          <div id="quux" class="foo"></div>
                                                          </div>


                                                          the selector .foo:last-child will match the divs #bar3 and #quux; but .foo:first-child or .foo:nth-child(1) will only match #quux because the first child of the first container is, again, not a .foo.



                                                          Source https://content.pivotal.io/blog/css-first-child-nth-child-and-last-child-are-not-like-eq






                                                          share|improve this answer












                                                          CSS :first-child, :nth-child, and :last-child are not like :eq



                                                          So if we had a snippet of HTML like



                                                          <div>
                                                          <div id="bar1" class="foo"></div>
                                                          <div id="bar2" class="foo"></div>
                                                          <div id="bar3" class="foo"></div>
                                                          </div>


                                                          Then the selector .foo:nth-child(2)will match the div #bar2. If we insert another element at the front of the container:



                                                          <div>
                                                          <p>Shift!</p>
                                                          <div id="bar1" class="foo"></div>
                                                          <div id="bar2" class="foo"></div>
                                                          <div id="bar3" class="foo"></div>
                                                          </div>


                                                          And again we select .foo:nth-child(2), we match the div #bar1 because the 2nd child of the container also matches .foo.



                                                          Thus, in this second example, if we try .foo:nth-child(1) or the equivalent .foo:first-child, we will not match any elements because the first child element in that container — the p tag — does not match .foo.



                                                          Likewise, :nth-child can match children in multiple containers. In the HTML snippet:



                                                          <div>
                                                          <p>Shift!</p>
                                                          <div id="bar1" class="foo"></div>
                                                          <div id="bar2" class="foo"></div>
                                                          <div id="bar3" class="foo"></div>
                                                          </div>

                                                          <div>
                                                          <div id="quux" class="foo"></div>
                                                          </div>


                                                          the selector .foo:last-child will match the divs #bar3 and #quux; but .foo:first-child or .foo:nth-child(1) will only match #quux because the first child of the first container is, again, not a .foo.



                                                          Source https://content.pivotal.io/blog/css-first-child-nth-child-and-last-child-are-not-like-eq







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Nov 22 at 4:03









                                                          nhphuc

                                                          17219




                                                          17219






















                                                              up vote
                                                              -2
                                                              down vote













                                                              :eq is array indexed based, so it starts from 0. It is also not part of the Css specification.



                                                              Whereas :nth-child is part of the Css specification and refers to the element position in a DOM tree.



                                                              In terms of usage, they both do the same thing.



                                                              Demo here






                                                              share|improve this answer



























                                                                up vote
                                                                -2
                                                                down vote













                                                                :eq is array indexed based, so it starts from 0. It is also not part of the Css specification.



                                                                Whereas :nth-child is part of the Css specification and refers to the element position in a DOM tree.



                                                                In terms of usage, they both do the same thing.



                                                                Demo here






                                                                share|improve this answer

























                                                                  up vote
                                                                  -2
                                                                  down vote










                                                                  up vote
                                                                  -2
                                                                  down vote









                                                                  :eq is array indexed based, so it starts from 0. It is also not part of the Css specification.



                                                                  Whereas :nth-child is part of the Css specification and refers to the element position in a DOM tree.



                                                                  In terms of usage, they both do the same thing.



                                                                  Demo here






                                                                  share|improve this answer














                                                                  :eq is array indexed based, so it starts from 0. It is also not part of the Css specification.



                                                                  Whereas :nth-child is part of the Css specification and refers to the element position in a DOM tree.



                                                                  In terms of usage, they both do the same thing.



                                                                  Demo here







                                                                  share|improve this answer














                                                                  share|improve this answer



                                                                  share|improve this answer








                                                                  edited Aug 12 '11 at 12:31

























                                                                  answered Aug 12 '11 at 12:26









                                                                  Tomgrohl

                                                                  1,677916




                                                                  1,677916






























                                                                      draft saved

                                                                      draft discarded




















































                                                                      Thanks for contributing an answer to Stack Overflow!


                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                      But avoid



                                                                      • Asking for help, clarification, or responding to other answers.

                                                                      • Making statements based on opinion; back them up with references or personal experience.


                                                                      To learn more, see our tips on writing great answers.





                                                                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                                      Please pay close attention to the following guidance:


                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                      But avoid



                                                                      • Asking for help, clarification, or responding to other answers.

                                                                      • Making statements based on opinion; back them up with references or personal experience.


                                                                      To learn more, see our tips on writing great answers.




                                                                      draft saved


                                                                      draft discarded














                                                                      StackExchange.ready(
                                                                      function () {
                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7039966%2fjquery-difference-between-eq-and-nth-child%23new-answer', 'question_page');
                                                                      }
                                                                      );

                                                                      Post as a guest















                                                                      Required, but never shown





















































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown

































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown







                                                                      Popular posts from this blog

                                                                      Lallio

                                                                      Futebolista

                                                                      Jornalista