views:

31

answers:

2

I have a list that looks like this:

<li class = "current_parent level-0">
  <a> Parent Link </a>
  <ul class= "children">
     <li class = "level-1">
           <a> Link to child </a>
     </li>

     <li class = "level-1 current_page_item">
          <a> Link to child </a>
     </li>

     <li class = "level-1">
           <a> Link to child </a>
     </li>

     </ul>
</li>

Basically, I want to get the HREF of the element one level UP and one level DOWN from the current_page_item .

I can get the URL of the current_page_item > a easily enough via .attr(), i.e. $('.current_page_item').attr("href");.

But how can I tell jQuery to look one level up? .parent() doesn't seem right, because they're not parents, they're siblings,right?

But how can I tell siblings to move UP or DOWN a list?

Basically I want $('.current_page_item > ONE LI > A UP').attr("href"); and $('.current_page_item > ONE LI > A DOWN').attr("href");.

Again, thanks for the help on my jQuery n00bquest!

+3  A: 

If you'r starting with the <li> use .next() and .prev() to get the next/previous siblings, for example:

var prevHref = $(".current_page_item").prev().find("a").attr("href");
var nextHref = $(".current_page_item").next().find("a").attr("href");

You can find a complete functions to move around like this as well as their descriptions under the Tree Traversal section of the jQuery API.

Nick Craver
A: 

use .prev() instead of .parent() (.prev() and .next() both refers to siblings)

jerjer