Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8089863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:32:39+00:00 2026-06-05T19:32:39+00:00

Fiddle is here – http://jsfiddle.net/ashwyn/a45ha/ HTML here – <div class=parent> <div class=a>Class A</div> <div

  • 0

Fiddle is here – http://jsfiddle.net/ashwyn/a45ha/

HTML here –

<div class="parent">
    <div class="a">Class A</div>
    <div class="b">Class B1</div>
    <div class="b">Class B2</div>
    <div class="b">Class B3</div>
    <div class="b">Class B4</div>
    <div class="b">Class B5</div>
</div>​

Jquery here –

$(function(){
    $(".parent").children(".b:nth-child(2)").css("color", "red");
});​

Above I have written .b:nth-child(2) so why I am not able to select second element of class="b" ?

I want to highlight Class B2 as color:red, but I am getting Class B1 as color:red. What is the problem guys ? If that is how it is works then according to me it is a bug in this type of selector.

The fiddle is just to let you know guys what is my problem but in my real problem I have having nth-child(2) as nth-child(j + 1) where j plays other role as well and it cannot make it j+2.

Can anyone please let me know the workaround to highlight class B2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:32:41+00:00Added an answer on June 5, 2026 at 7:32 pm

    why don’t you use :eq(2) instead of :nth-child(2).
    In order to get B2 you would write:

    $(".parent").children(".b:eq(1)").css("color", "red");
    

    Update

    For your original question why :nth-child(2) is not selecting the second child, I found the reason. On jQuery docs site i found:

    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.

    So in your case

    $(".parent").children(".b:nth-child(2)").css("color", "red");

    the :nth-child(2) counts the children of .parent and returns only if the element is .b

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Fiddle here: http://jsfiddle.net/cgweb87/r3NEX/ When you click on the h2, it slides the div up
You can see the fiddle here: http://jsfiddle.net/easeS/4/ Here is the html/css I have: #main
Please review the fiddle here for an example: http://jsfiddle.net/TKJsc/1/ Basically i'm trying to make
In the fiddle here http://jsfiddle.net/mjmitche/KvwGw/21/ , when you submit the form, a new form
I have created fiddle here: http://jsfiddle.net/ozzy/WEGMh/ How it should look is here: Issue is
Fiddle here: http://jsfiddle.net/csaltyj/3A78u/ I want the sub-sub nav menu to align with the top
Fiddle here: http://jsfiddle.net/emJcx/1/ I have a drop down menu that is a simple show
In the fiddle here: http://jsfiddle.net/maniator/8Lhns/ if you click on any of the links (like
I have created a fiddle here: http://jsfiddle.net/ozzy/paPRg/1/ Basically, I want to allow users to
I have the following fiddle here: http://jsfiddle.net/9jN8L/ The idea is that sidebar will be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.