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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:07:13+00:00 2026-05-18T21:07:13+00:00

I try to find a next or prev element of current element. But next()

  • 0

I try to find a next or prev element of current element. But next() and prev() function can only work in a scope, it can not reach outside. For an example this is what I want to achieve:

<ul id="ul1">
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>
    <ul id="ul2">
      <li>4</li>
      <li>
        <ul id="ul3">
          <li>5</li>
          <li>6</li>
        </ul>
      </li>
      <li>7</li>
      <li>8</li>
    </ul>
  </li>
  <li>9</li>
</ul>

If current element is ul1, next element is <li>1</li>, prev element is null.

If current element is <li>1</li>, next element is <li>2</li>, prev element is ul1

If current element is <li>8</li>, next element is <li>9</li>, prev element is <li>7</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-05-18T21:07:14+00:00Added an answer on May 18, 2026 at 9:07 pm

    First of all, this was a really neat problem. Thanks for posting!

    I accomplished this with a chained jQuery statement. Only thing is, if you run previous() on the top ul you’ll get body. Which I think technically makes sense. However, if this isn’t the desired behavior, see my update below.

    Usage: next("#ul3") returns <li>5</li>

    Next:

    function next(selector) {
        var $element = $(selector);
    
        return $element
            .children(":eq(0)")
            .add($element.next())
            .add($element.parents().filter(function() {
                return $(this).next().length > 0;
            }).next()).first();
    }
    

    Previous:

    function previous(selector) {
        var $element = $(selector);
    
        return $element
            .prev().find("*:last")   
            .add($element.parent())    
            .add($element.prev())
            .last();     
    }
    

    Update If you want to limit the upper most node previous can be, you could do:

    function previous(selector, root) {
        var $element = $(selector);
    
        return $element
            .prev().find("*:last")   
            .add($element.parent())     
            .add($element.prev())
            .last().not($(root).parent());      
    }
    

    http://jsfiddle.net/andrewwhitaker/n89dz/

    Another Update: Here’s a jQuery plugin for convenience:

    (function($) {
        $.fn.domNext = function() {
            return this
                .children(":eq(0)")
                .add(this.next())
                .add(this.parents().filter(function() {
                    return $(this).next().length > 0;
                }).next()).first();        
        };
    
        $.fn.domPrevious = function() {
            return this
                .prev().find("*:last")   
                .add(this.parent())     
                .add(this.prev())
                .last();         
        };
    })(jQuery);
    

    Expanded example here: http://jsfiddle.net/andrewwhitaker/KzyAY/

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

Sidebar

Related Questions

Perhaps I'm not using the right terminology, but I'm struggling to try to find
Try to find the solution but i cant. So problem is next one. I
I really try to find on google how can we convert .doc and .txt
I am trying to make a find, find next function for my program, which
Guys, sorry for the basic question. I try to find out but I still
I'm trying the next code to try to see if predict can help me
When I try to find out the total count of delicious bookmarks, Delicious returns
I have used DLL Export viewer to try and find the functions that are
I have been through every option to try to find a way to get
I have an Java AST and I try to find a variable inside it

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.