Meds86 Meds86 - 4 months ago 9
jQuery Question

Traverse dom, next, prev

I'm trying to get the next slide element attr

href
after the active current one.

<ul class="nav">
<li class="active"><a href="#slide01"></a></li>
<li><a href="#slide02"></a></li>
<li><a href="#slide03"></a></li>
<li><a href="#slide04"></a></li>
</ul>


To get the current one I do

slideActive = $('.nav li.active a').attr('href')
which is good it gives
#slide02
, however I'm not sure how to get the
href
of the previous or next
li
elements.

Answer

Try this:

1.for selecting the href of the next element

 $('.nav li.active').next('li').find('a').attr('href')

2.for selecting the href of the previous element

$('.nav li.active').prev('li').find('a').attr('href')