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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:09:58+00:00 2026-05-25T22:09:58+00:00

<div id=container> <div class=aa></div> <div class=bb></div> <div class=aa></div> <div class=bb></div> <div class=bb></div> <div class=bb></div>

  • 0
<div id="container">
  <div class="aa"></div>
  <div class="bb"></div>
  <div class="aa"></div>
  <div class="bb"></div>
  <div class="bb"></div>
  <div class="bb"></div>
  <div class="bb"></div>
</div>

i just need to find the index of element based on particular class while looping.

Normal index will give the element index no: among the siblings.
I want to consider siblings with same class only, and give the index based on that sorted siblings.

$("#container div").each(function(){

    if($(this).hasClass('bb')){
        var index = $(this).index();
            //will give me 1,3,4,5 etc

        //I want 0,1,2 etc ie: list the index based on class 

      }
    if($(this).hasClass('aa')){
           var index = $(this).index();

            //will give me 0 , 2
      //I want 0,1 etc ie: list the index based on class 
      }

})
  • 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-05-25T22:09:59+00:00Added an answer on May 25, 2026 at 10:09 pm

    For .index() you can pass a selector as a parameter (for example '.bb').

    As you can find in the online documentation.

    If a selector string is passed as an argument, .index() returns an
    integer indicating the position of the original element relative to
    the elements matched by the selector. If the element is not found,
    .index() will return -1.

    So you could do something like:

    $("#container div").each(function(i){
    
        if($(this).hasClass('bb')){
            var index = $(this).index('.bb');
        }
        else if($(this).hasClass('aa')){
            var index = $(this).index('.aa');
        }
    
    });
    

    jsFiddle Demo

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

Sidebar

Related Questions

I need to add another div around items with class .item <div id=container> <div
I have an image <div class='container'> <img src='../images.jpg' alt='images' /> </div> I want to
I have an animation on a partially hidden div container that will execute when
I have some asp textboxs in a div container. I just want to clear
In the theme which i'm using has this code <div id=header> <div class=container section
Ok i got this situation <div id=container> <img src=images/4.png /> <ul class=menu> <li id=menu-0>
I have a div container and have defined its style as follows: div#tbl-container {
I have a DIV container with several inner DIVs layed out by floating them
Is there any way to disable all input fields in an div container with
I am using jQuery to show/hide a div container (#pluginOptionsContainer), and load a page

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.