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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:26:45+00:00 2026-05-29T23:26:45+00:00

I need to go through elements with previuous and next buttons. Here is HTML

  • 0

I need to go through elements with previuous and next buttons. Here is
HTML :

    <ul>
        <li><a href="#">Number 1 item</a></li>
        <li><a href="#">Number 2 item</a></li>
        <li><a href="#">Number 3 item</a></li>
        <li><a href="#">Number 4 item</a></li>   
    </ul>
<input type="button" class="next" value=">" />
<input type="button" class="prev" value="<" />

JS :

(function(){
    var $lis = $('ul li');
    var index = 0, lastIndex = 0;

    start(); // activate first, add event listeners to buttons

    function next(){
        lastIndex = index;
        if(++index == $lis.length) index = 0; // if next is out of bounds go to first
        $lis.eq(index).addClass('active');
        $lis.eq(lastIndex).removeClass('active');
    };

    function prev(){
        lastIndex = index;
        if(--index < 0) index = ($lis.length - 1); // if prev is less than first to to last
        $lis.eq(index).addClass('active');
        $lis.eq(lastIndex).removeClass('active');
    };
    function start(){
        $lis.eq(0).addClass('active');
        $('.next').click(next);
        $('.prev').click(prev);
    }
})();

This plugin works perfect, Here is Fiddle

But I want to extend its functionality. I also want to enable clicking on list directly & remember its index.

What I mean is : Say, I reached till item no.3 while clicking on next button, & if I directly click on
<li><a href="#">Number 1 item</a></li> (item no:1) then I want to remember my this position as current index & when I click on next button, I should move to item no.2

same thing also goes with previous button.

How can I extend this functionality ?

  • 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-29T23:26:46+00:00Added an answer on May 29, 2026 at 11:26 pm
    function li_click(){
        lastIndex = index;
        index = $(this).index();
        $lis.eq(index).addClass('active');
        $lis.eq(lastIndex).removeClass('active');
    }
    
    function start(){
        $lis.eq(0).addClass('active');
        $('.next').click(next);
        $('.prev').click(prev);
        $lis.click(li_click);
    }
    

    Here is an updated version of your JSFiddle: http://jsfiddle.net/Rtmdj/15/

    Also it doesn’t look like you really need the lastIndex variable, you can just remove the active class, change the index, then add the active class to the new index:

    $lis.eq(index).removeClass('active');
    index = $(this).index();
    $lis.eq(index).addClass('active');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to go through elements with previuous and next buttons but am having
I'm experimenting with filtering through elements in parallel. For each element, I need to
I need to loop through a set of 2-dimensional array of hidden input fields
I need to loop through some elements in the page and then, for each
I need to loop through all table elements, somewhere keeping an index and then
I am looping through some elements and need to determine if an element has
I need to count the number of elements in an XML file that have
I need to iterate through the elements in a numpy array so I can
I have a Dictionary whose elements I need to iterate through and make changes.
I need to represent a series of elements through time. For designing purposes each

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.