I have the following menu, which can contain up to 50 elements and two links that the user can use to go to the previous or next city. Here for this example, I just show four address links.
<ul id="menu"> <li><a href="/City/0101004H">1</a></li> <li><a href="/City/0101004I">2</a></li> <li><a href="/City/0101004J">3</a></li> <li><a href="/City/0101004K">4</a></li> </ul> <a id="prev" href="#"><img width="16" height="16" src="/images/prev.png">Prev</a> <a id="next" href="#"><img width="16" height="16" src="/images/next.png">Next</a>
I had some help with stack overflow and the following code appeared:
fiddle
$("#menu").on('click', 'a[href^="/City"]', function(event) { event.preventDefault(); var prev = $(this).parent().prev().find('a'); var next = $(this).parent().next().find('a'); $('#prev').prop('href', jQuery(prev).prop('href')).prop('title', 'City ' + jQuery(prev).text()); $('#next').prop('href', jQuery(next).prop('href')).prop('title', 'City ' + jQuery(next).text()) });
This sets the href value of the previous and next value when I click one of the menu items. It works, but not for the first and last items in the list. I need the following:
a) When the first element is clicked, I would like #prev to change to:
<a id="prev" href="#"><img src="/images/noentry.png"></a>
b) When clicking the last item, I would like #next to change to:
<a id="next" href="#"><img src="/images/noentry.png"></a>
Is there an easy way to do this without adding extra code?
Samantha JT Star
source share