Madam Zu Zu
Madam Zu Zu

Reputation: 6615

Getting Selected to show on the JQuery Menu LI element

I am trying this:

function initMenu() {
    $("#menu ul").hide();
    $("#menu li a").click(function() {
        $(this).addClass('selected');
        $(this).next().slideToggle('normal');
    });
}
$(document).ready(function() {
    initMenu();
});

but it adds class="selected" to the <a> attribute... how can I add it to the current <li> instead?

Upvotes: 1

Views: 2698

Answers (3)

Chandu
Chandu

Reputation: 82953

Use jQuery parent() function to access li:

function initMenu() {
    $("#menu ul").hide();
    $("#menu li a").click(function() {
    $(this).parent().addClass('selected');
        $(this).next().slideToggle('normal');
    });
}

Upvotes: 1

Adam
Adam

Reputation: 44969

Use parent

function initMenu() {
        $("#menu ul").hide();
        $("#menu li a").click(function() {
            $(this).parent().addClass('selected');
            $(this).next().slideToggle('normal');
        });
    }
    $(document).ready(function() {
        initMenu();

    });

Upvotes: 1

Jason Benson
Jason Benson

Reputation: 3399

You're clicking the "A" so your class is added to that element. However it appears you want it added to the parent of your a tag so...

$("#menu li a").click(function() {
    $(this).parent().addClass('selected');
        $(this).next().slideToggle('normal');
    });

Upvotes: 4

Related Questions