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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:24:54+00:00 2026-06-05T21:24:54+00:00

I this: http://jsfiddle.net/ZP76c/ I’m trying to control the scope at which jQuery selects elements

  • 0

I this: http://jsfiddle.net/ZP76c/

I’m trying to control the scope at which jQuery selects elements based off their index(), possible?

<div class="holder">
    <div class="first">First</div>
    <div class="second">Second</div>
    <div class="first">First</div>
    <div class="second">Second</div>
</div>​

$('.holder div').click(function(){
     alert($(this).index());            
});

// desired behaviour: clicking the first 'first' div will alert: "0"
// clicking the second 'first' div, will alert: "1"
// so it takes the divs with a class of 'second' out of the index() calculation
// possible with jQuery .index()?
​
  • 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-05T21:24:56+00:00Added an answer on June 5, 2026 at 9:24 pm

    The following should suit your needs:

    $('.holder div.first').click(function() {
        alert($(this).index(".first"));            
    });
    

    Or for a more general solution (so .second does the same):

    $('.holder div').click(function() {
        alert($(this).index("." + $(this).prop("class")));            
    });
    

    This only works though, if each div only has one class, and the class is “first/second/third” etc.

    And in case that your divs had several classes, you could add a data attribute instead, and then do something like the following:

    $('.holder div').click(function() {
        alert($(this).index("[data-category=" + $(this).attr("data-category") + "]"));            
    });
    

    and the HTML:

    <div class="holder">
        <div class="first" data-category="first">First</div>
        <div class="second class-does-not-matter" data-category="second">Second</div>
        <div class="first another-div-class" data-category="first">First</div>
        <div class="second div-class" data-category="second">Second</div>
    </div>​​​​​​​​​​​​​​​​​​
    

    Where data-category was set to whatever category it was a member of.

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

Sidebar

Related Questions

I have elements structured roughly like this: http://jsfiddle.net/zyySd/ Is there any way to achieve
I have created a fiddle to test this : http://jsfiddle.net/Ninjanoel/X6ShS/ which works fine :(
Compare this http://jsfiddle.net/AndyMP/tbYsM/1/ which works with this http://jsfiddle.net/AndyMP/tbYsM/2/ which doesn't. The first is just
Please refer to this: http://jsfiddle.net/k8c7w/ I am trying to get the middle grey band
Im trying to accomplish this: http://jsfiddle.net/WkVb9/ But instead of using a textarea, use an
I have a jsFiddle set up for this: http://jsfiddle.net/eventide/TAmam/ I'm trying to apply the
Wonder if anyone could shed some light on this http://jsfiddle.net/orbitalmedia/ZrxBb/7/ Basically I'm trying to
I want to make a highchart like this http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/line-labels/ I have a stored procedure
I am trying to transform this: http://jsfiddle.net/pljehyun/jqUg3/ into this: (this question's main concern is
could somebody please take a look at this http://jsfiddle.net/bloodygeese/EzkFR/1/ My aim is to on

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.