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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:39:16+00:00 2026-06-09T18:39:16+00:00

I am trying to carry a class between li ‘s with combine each() and

  • 0

I am trying to carry a class between li‘s with combine each() and eq() methods when buttons clicked. I am using same code with previous and next buttons except i+1 and i-1 but it is returning me different problems.

Here is jsFiddle to examine.

html:

<span class="prev">prev</span>
<ul>
    <li>0</li>
    <li>1</li>
    <li class="active">2</li>
    <li>3</li>
    <li>4</li>
</ul>
<span class="next">next</span>

jQuery:

var li = $('li');

$('.prev').click(function() {
    li.each(function(i) {
        if ( $(this).hasClass('active') ) {
            console.log(i);
            //this returning current true value = 2

            li.removeClass('active');
            li.eq(i-1).addClass('active');
            //this is working better
            //problem is: not selecting 4
        }
    });
});

$('.next').click(function() {
    li.each(function(i) {
        if ( $(this).hasClass('active') ) {
            console.log(i);
            //this returning current true value = 2

            //problem starts:
            //li.removeClass('active');
            //when this is active; all active classes are gone

            li.eq(i+1).addClass('active');
            //try to select next 'li' here
            //but it is selecting all li's bigger than current value
        }
    });
});
  • 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-09T18:39:17+00:00Added an answer on June 9, 2026 at 6:39 pm

    If for some reason you need the index, you could always do:

    $('.prev').on('click', function() {
        var i = $(".active").index();
            i--;
        $(".active").removeClass('active');
        $('li').eq(i).addClass('active');
    });
    
    $('.next').on('click', function() {
        var i = $(".active").index();
            i = i >= $('li').length-1 ? 0 : i+1;
        $(".active").removeClass('active');
        $('li').eq(i).addClass('active');
    });​
    

    FIDDLE

    If not :

    $('.prev, .next').on('click', function() {
        if ($(".active")[$(this).attr('class')]().index()!=-1)
        $(".active").removeClass('active')[$(this).attr('class')]().addClass('active');
    });
    

    FIDDLE

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

Sidebar

Related Questions

I currently trying to carry out the same operation (OCR) on images in android,
I'm simply trying to carry a string onto the next activity without having to
I am trying to write an std::iterator for the CArray<Type,ArgType> MFC class. This is
I'm using NetBeans as my IDE. Whenever I have some code that uses another
I am trying to pass an argument asins to each form in a modelformset,
I'm trying to carry over player_id and save to the Stakes table referencing that
I'm trying to carry out a jsp page with some functionalities of Jquery. But,
I have the following code: template<typename T, typename Allocator = std::allocator<T> > class Carray
When I am trying to carry out following statement from Management Studio, it is
Possible Duplicate: about assembly CF(Carry) and OF(Overflow) flag I'm trying to see if carry

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.