here is html:
if I press 'a' link it will addClass 'current' to clicked li and remove old li class? this is my try:
$('ul li a').click(function(){ $('ul li').removeClass('current'); ... don't know here (add class to current li) ... });
here is html:
if I press 'a' link it will addClass 'current' to clicked li and remove old li class? this is my try:
$('ul li a').click(function(){ $('ul li').removeClass('current'); ... don't know here (add class to current li) ... });
Use .parent('li')
or .closest('li')
to select the clicked
$('ul li a').click(function() { $('ul li.current').removeClass('current'); $(this).closest('li').addClass('current'); });
Use on('click') if you are using latest version of jquery
$('ul li a').on('click', function(){ $(this).parent().addClass('current').siblings().removeClass('current'); });
A fiddle is here.
$('ul li a').click(function(){ $('ul').children().removeClass('current'); $(this).addClass('current'); });
Hope, this will works for you..
Rob is right but $('ul li.current').removeClass('current');
didn't work for me. Following code works.
jQuery('ul li a').click(function() { jQuery('ul').children().removeClass('selected'); jQuery(this).closest('li').addClass('selected');
}
Just add the the class to this
like so...
$(this).addClass('yourClass');
Maybe you mean this?
$('ul li').each(function(){ var t = $(this); t.find('a').click(function() { $('ul li').removeClass('current'); t.addClass('current'); }); });