Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript jQuery: add a class to parent of child < li >

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #36293
    sly
    Participant

    I want to add a class to the < li > ‘About Us’ because it is the parent nav of the < li class="current_page" >

    This jQuery gets me real close:


    It doesn’t quite get there because it only gets as far as the < ul > above it ie. the parent.

    Can I add something to the jQuery that selects the < li > above the parent < ul > of the < li class="current_page"> ?

    #95346
    TheDoc
    Member

    I’m no jQuery expert, but could you just do this?

    #95348
    sly
    Participant

    No jQuery expert??? I disagree… you are a jQuery master!!!

    Thank you so much!

    That works perfectly no matter how many < li > elements there are before the parent!

    #95350
    TheDoc
    Member

    haha no problem!

    #95351
    sly
    Participant

    hmm…

    sadly it works on the static list I created above but not on the dynamic list created by my cms. Not sure why.

    #95352
    TheDoc
    Member

    Hmmm… make sure the script is being run at the bottom of the page. Also, you may need to replace the ‘$’ in front with ‘jQuery’. Like I said, not an expert! ;)

    #95369
    jamygolden
    Member

    The best jQuery selector function would probably be closest(). closest() basically goes up through all parents until it finds a match. So:

    $('li.current_page').closest("li").addClass("this_is_the_childs_nav_parent");

    That would match the first list item ancestor and add the class.

    #95372
    TheDoc
    Member

    Look at you smart jQuery guys!

    #95378
    sly
    Participant

    yes! thanks all!

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.