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

  • Home
  • SEARCH
  • 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 1087819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:59:20+00:00 2026-05-16T22:59:20+00:00

I need to select a specific grange of jquery elements based on there index

  • 0

I need to select a specific grange of jquery elements based on there index using :eq() (or something else if you have a better solution)

my html structure is the following:

<ul>
  <li>slide0</li>
  <li>slide1</li>
  <li>slide2</li>
  <li>slide3</li>
  <li>slide4</li>
</ul>

When the user hovers slides over the slide2 i need to select li:eq(0), li:eq(1) and li:eq(3), li:eq(4) separatly, because they have a different animation.

This is my solution, but this feels a little messy…

var $slides, theOthers, slidesTotal;
$slides = $('ul > li');
slidesTotal = $slides.length;
theOthers = function(slideIndex ,slidesTotal){
   var before = [], after = [], i=0;
   while (i<=slideIndex - 1){
       before[i] = ":eq(" + i + ")"
       i++
   };
   while (i <= slidesTotal) {
       after[i] = ":eq(" + i + ")"
       i++
   };
   return [ before.join(",") , after.join(",") ]
}

$slides.mouseenter(function(){
   var groups, slideIndex, $that = $(this);
   slideIndex = $that.index();
   groups = theOthers(slideIndex, slidesTotal);
   $slides.filter(groups[0]).dosomething();
   $slides.filter(groups[1]).dosomethingelse()
})

is there a more simple way to do this with jQuery?

  • 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-16T22:59:20+00:00Added an answer on May 16, 2026 at 10:59 pm

    Using .prevAll() and .nextAll() you can reduce all of your code in the question down to this:

    $('ul > li').mouseenter(function() {
       $(this).prevAll().dosomething();
       $(this).nextAll().dosomethingelse();
    });
    

    .prevAll() gets all previous siblings, and .nextAll() gets all following siblings, you can do what you want to each set.

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

Sidebar

Related Questions

I've been using CSS3 selectors to select specific elements based on their attributes, although
I have to select specific column from my DataTable using linq I am using
So I have a large form and I need to select all elements that
I need to select node which has a specific value: <td class='formlabel'>Name:</td> So I
how can i express in xpath a) need select elements which contains specified keyword.
I need to select elements without child node (including text since in <p> text
I need to select elements of a certain class except children of $(this). The
I need to select elements depending on first letter of text value of a
I need to select a row from the table with the specific id, but
I have a table: ComputerID, UserID, LoginDate I need a select statement that returns:

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.