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 8093357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:29:15+00:00 2026-06-05T20:29:15+00:00

So I have a div with some child elements and when I select one

  • 0

So I have a div with some child elements and when I select one with jQuery I want to get the index of it within a selector

<div>
  <div class="red"></div>
  <div class="red"></div>
  <div class="red"></div>
  <div class="blue"></div>
  <div class="red"></div>
  <div class="blue"></div>
  <div class="blue"></div>
  <div class="red"></div>
</div>

So lets say that I have the last element in the main div selected. If I call index() on it it will give me ‘7’ because out of all the child elements the index is ‘7’. But now lets say I want to get the index based on the other ‘red’ elements, the goal is to return a value of ‘4’ because out of all of the ‘red’ elements it is the fifth one. I looked through the documentation and didnt find a whole lot, then I experimented with putting selectors in the index() method like index(‘.red’) but I couldn’t get anything working.

  • 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-05T20:29:16+00:00Added an answer on June 5, 2026 at 8:29 pm

    Well, the documentation says:

    .index( element )
    element The DOM element or first element within the jQuery object to look for.

    So can do:

    selectedElements.filter('.red').index(this);
    

    If you don’t have selectedElements already, you can select corresponding siblings with, for example:

    $(this).parent().children('.red')
    

    If every element has only one class and then the filter can be dynamic:

    var index = $(this).parent().children('.' + this.className).index(this); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some floated div's and I want to select the third with jquery.
I have some nested elements as such: <div id=knownID> <div class=knownClass> <span> <nobr> <span
I have this div in my code: <div id=advice class=validation-advice style=>some text here</div> Now,
I have a div which contains 2 child elements. The 1st div have content
In my HTML there are a lot of elements. Some div elements have an
I have a bunch of child elements that are uniquely identified within a parent
Here is my scenario. I have a container div that has (n) child elements
I have Div that it contains some other elements like <p> , <ul> ,...
I have a div with some inner content that I need to have an
I have a div with float:right (no position property declared). This div has some

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.