I'm having troubles traversing an array in jQuery. I want to make navigation (next & prev) for pages based on an li that I have on the page. I'd like to grab the hrefs from the list on the page, compare them with the url of the current page, then make a link to the previous and next pages from the list.
// Get the current page url:
var currentUrl = window.location.href;
var currLink = $('#tracks').find('a[href="' + currentUrl + '"]');
// Get the index:
var index = curLink.parent().index() + 1; // (+1) means 'next'
// Get the next `li > a` href:
var nextPage = currLink.parent().next().find('a').attr('href');
// Show the index & the href value:
$('.nav').append(index + ': ' + nextPage);
I'm having troubles traversing an array in jQuery. I want to make navigation (next & prev) for pages based on an li that I have on the page. I'd like to grab the hrefs from the list on the page, compare them with the url of the current page, then make a link to the previous and next pages from the list.
Here is what I've done so far, and am stuck...
HTML:
jQuery:
I just can't figure out how to get the hrefs from track 2 and track 4 and append links to the nav. Any help would be greatly appreciated!
Thank you!
My code wasn't showing in the original post. This is the HTML I'm working with.
Hompimpa,
Thanks for the help! I understand what you've done. I will go try it out now.
Thanks!
-Mike