Treehouse: Grow your CSS skills. Land your dream job.

jquery – get prev and next href.

  • # May 14, 2012 at 5:36 pm


    I’m trying to get hold of the prev/next-objects when clicking a link from within the #menu.
    I’ve tried all sorts of .prev(), .prevUntil() a.s.o but so far I haven’t had any luck with it :)

    My current layout is:


    $('#menu a').click(function(){
    // get the parent.a object so i can send send it to the #prev container as pageX
    // get the next.a object so i can send it to the #next container as pageX

    If possible the script should ignore the table,tr,td,ul & li items, the menu structur/html can change in the future (its can be user-configured) – if not, no worries :)

    # May 14, 2012 at 8:34 pm
    $('#menu a').click(function(){
    var theLink = $(this).parent().siblings().children('a').attr('href');
    // do whatever you want with the theLink variable
    # May 15, 2012 at 7:13 am

    Kinda like that yes, except that it only gets the data in the same td.
    Firebug says:

    clicked: page1 > prev = page2
    clicked: page2 > prev = page1
    clicked: page3 > prev = undefined
    $('#menu a').click(function(){
    $prev = $(this).parent().siblings().children('a').attr('href');
    console.log( "clicked: " + $(this).attr('href') + " > prev = " + $prev );
    return false;

    The idea is that clicking “page2″ will set “prev” = “page1″ and “next” = “page3″, clicking “page1″ will set “next” to “page2″ and “prev” to null, and so on.

    I managed to make a “hey it works” version yesterday that runs a $(‘#menu a’).each and then sets the $prev and $next, but it feels a bit clumsy and php’ish.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.