jQuery find next and prev element

Posted by StoneHeart on Stack Overflow See other posts from Stack Overflow or by StoneHeart
Published on 2010-12-23T23:01:48Z Indexed on 2010/12/23 23:54 UTC
Read the original article Hit count: 245

Filed under:

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>

© Stack Overflow or respective owner

Related posts about jQuery