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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:57:29+00:00 2026-06-17T13:57:29+00:00

How to find next and previous A element from #current using jQuery. HTML: <ul>

  • 0

How to find next and previous “A” element from “#current” using jQuery.
HTML:

<ul>
    <li>
        <a></a>
        <a></a>
        <a></a>
    </li>
    <li>
        <a></a>
        <a></a>
        <a id='current'></a>
    </li>
    <li>
        <a></a>
        <a></a>
        <a></a>
    </li>
</ul>

i’ve tried to create a code with something like this:

var curr = $('#current');
var prev = curr.parent().prev('a');
var next = curr.parent().next('a');

But it didn’t work. I think it’s a very easy questions, but i’m stuck right now. Any ideas?

UPD: It should travel to next “LI” element to find “A” if it’s the last “A” element in current “LI”.

  • 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-17T13:57:30+00:00Added an answer on June 17, 2026 at 1:57 pm

    You do not need to use parent to get prev and next

    Live Demo

    var curr = $('#current');
    var prev = curr.prev('a');
    var next = curr.next('a');
    

    To make this cross row you need to check if you have current element first or last of row. If current element is first then last would be last element for previous row and similarly if current element is last of row then next will be first of next row.

    Live Demo

    $('a').click(function () {
        $('ul > li > a').css('background-color', 'white');
        var curr = $(this);
        curr.css('background-color', 'green');;
        var prev = curr.prev('a')
        if(prev.length == 0 )
        {
            lastaOfPrevLi = curr.parent().prev();
            if( lastaOfPrevLi.length != 0)
                lastaOfPrevLi.find('a:last').css('background-color', 'red');
        }
        else
            prev.css('background-color', 'red');
        var next = curr.next('a');
        if(next.length == 0 )
        {
            lastaOfNextLi = curr.parent().next();
            if( lastaOfNextLi.length != 0)
                lastaOfNextLi.find('a:first').css('background-color', 'orange');
        }
        else
                next.css('background-color', 'orange');
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using Java LinkedList how do you find out the element's next or previous
How does this code work to Find the next highest power of 2 for
I have a Time object and would like to find the next/previous month. Adding
How do i find the previous and next element of any given element in
Possible Duplicate: jQuery: Checking if next element exists I'm implementing a custom slider using
I remapped [[ and ]] to find the previous and next pattern in the
To find the next odd number for an input the following code is being
How can I find the next or previous <a> regardless of where it is
I can't think of a simple and elegant way to find previous and next
function xmlParser(xml, projectName) { var currentIndex = 0; $(xml).find('entry').each(function(){ if($(this).attr('projectName').toLowerCase() == projectName) { $previous

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.