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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:07:29+00:00 2026-05-30T19:07:29+00:00

I need to be able to dynamically determine what the index of a certain

  • 0

I need to be able to dynamically determine what the index of a certain div inside another div is. For example:

<div id="parent">
    <div id="div-0"></div>
    <div id="div-5"></div>
    <div id="div-1"></div>
    <div id="div-2"></div>
    <div id="div-3"></div>
    <div id="div-4"></div>
</div>

For any of those I need to be able to know if a given div is the first, second etc…

EDIT:
Ok obviously I haven’t explained this well. I tried all of the below and all I get is 0 every time. I have edited the example above to reflect what is happening. So given the updated example what I need to be able to do is the following:

var index = $('#div-4').index();

Currently this is only returning 0 every time.

  • 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-30T19:07:30+00:00Added an answer on May 30, 2026 at 7:07 pm

    playground

       $('#parent div').on('click',function(){     
          var elIndex = $(this).index();      
          $(this).text('my index is: '+ elIndex);     
       });
    

    http://api.jquery.com/index/


    Good to know that in jQuery v lower that 1.3.3 .index() will not work,
    in that cases you can use .prevAll() and length:

    var elIndex = $(this).prevAll().length;
    

    This technique is specially useful if you have more than one class name as children, e.g. inside a DIV you have:

    <div id="parent">
        <div class="aa"></div>
        <div class="bb"></div>
        <div class="aa"></div>
        <div class="bb"></div>
    </div>
    

    … and clicking on a ‘.aa’ element you want to get it’s ‘index’ than it would be something like:

    $('#parent .aa').click(function(){  
      var elIndex = $(this).prevAll('.aa');  
      $(this).text('my index type of class name is: '+ elIndex);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Rails plugin, and need to be able to dynamically determine which
I need to be able to add rows to a table dynamically with jQuery
(Summary: My users need to be able to edit the structure of their dynamically
What's the difference? In my context, I need to be able to dynamically add
I am in need of creating a dynamically extend-able class in C#. The goal
I need to be able to dynamically modify an NSMenu hierarchy each time it
I want to be able to dynamically place image(s) over another image in my
I need to be able to dynamically populate a html select base on input
I'm using the jQuery plugin colorbox and I need to be able to dynamically
Users of the website need to able to store images in their area ,

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.