Alex Rivera | Logout

jQuery find next and prev element

Asked 2010-12-23T23:01:48.950
18

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>

Edit
Report

1 Answer

15

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(
answered 2010-12-24T02:00:29.360

Your Answer