Forums

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

Home Forums JavaScript Add class to parent of submenu

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #30556
    carlgordon
    Member

    Hi

    I’m stuck on adding a class to a submenu’s parent using javascript. Adapting Chris’s dynamic page script here is what I have so far…

    $(function() {
    var newHash = "",
    $mainContent = $("#main-content"),
    $el;

    $("#nav li").delegate("a", "click", function() {
    window.location.hash = $(this).attr("href");
    return false;
    });

    $(window).bind('hashchange', function(){

    newHash = window.location.hash.substring(1);

    if (newHash) {
    $mainContent
    .find("#guts")
    .fadeOut(200, function() {
    $mainContent.hide().load(newHash + " #guts", function() {
    $mainContent.fadeIn(200, function() {});
    $("#nav li a").removeClass("current");
    $("#nav li a[href="+newHash+"]").addClass("current");
    });
    });
    };
    });
    $(window).trigger('hashchange');

    });

    I’ve been Googling it all day but can’t find a simple solution. Here is my HTML…


    Thanks

    #77909
    armin.hackmann
    Participant

    Hi Carl,

    this line should work:

    $("#nav li a[href="+newHash+"]").addClass("current").parents("ul.subnav").siblings("a").addClass("current");

    I think, it explains itself. If not, feel free to tell me so & i will try to write it up. Just dont have the time right now…

    Greets
    Armin

    #77952
    carlgordon
    Member

    Thank you very much. Just one small correction, I had to change the (“ul.subnav”) to (“ul#subnav”). But otherwise perfect, just what I was looking for.

    Cheers

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