- This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
Viewing 4 posts - 1 through 4 (of 4 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › jQuery Array
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:
$('a').each(function(index, value) {
var currentPage = "http://www.mydomain.com/track3/";
if (value == currentPage){$('.nav').append(index + ': ' + value);}
});
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!
// 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);
Hompimpa,
Thanks for the help! I understand what you’ve done. I will go try it out now.
Thanks!
-Mike