Reputation: 6615
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
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
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
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