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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:41:34+00:00 2026-06-17T09:41:34+00:00

Expected result is to get red all special divs between first and last selected

  • 0

Expected result is to get red all special divs between first and last selected div.

This works but couldnt be it better written?:

var s = $('div.selected'),
    $spec = $('.speial'),
    i = s.first().index('.speial'),
    last = s.last().index('.speial');
for(;i<last;i++){
  $spec.eq(i).addClass('gored');
}

I was trying this:

var s = $('div.selected'),
    $spec = $('.speial'),
    i = s.first().index($spec),
    last = s.last().index($spec);
for(;i<last;i++){
  $spec.eq(i).addClass('gored');
}

But it doesnt work… I am interested in the reason why.
Or maybe is there even better way to achieve the same result?

EDIT: I have made the fiddle to get picture of the problem 🙂 fiddle

  • 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-17T09:41:35+00:00Added an answer on June 17, 2026 at 9:41 am

    jQuery API doc:

    If .index() is called on a collection of elements and a DOM element or
    jQuery object is passed in, .index() returns an integer indicating the
    position of the passed element relative to the original collection.

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

    So It seems I have firstly misunderstood the relativity in both cases, so I thought that “elements matched by the selector” could be replaced by selection (jQuery object).

    The answer is that .index() works another way than I expected.

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

Sidebar

Related Questions

I want to get the result from the below table like this ..., Expected
I am wondering about why I don't get the expected result with this one:
My expected result is: Hello world! but when i using below codes: MainDocumentPart mainDocumentPart
Why do I get this result when doing a trim() ? trim( '18 x
Node's HTTP module is not returning all the expected data from this URL: http://itradedata.co.za/pmchart/prices/GetPricesTRI.asp?Shortname=ACE&numprices=5
Steps: Run the build including unit tests. Expected result: the unit tests are executed
The example code of section 10.6, the expected result is: after several iterations, the
Update: I've corrected the post, so the question is closed. Expected result: Menu width
Expected output & output I get in debug mode, and release mode under VS2010,
I am confused from the result of the following code: I can't get my

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.