views:

22

answers:

3

I need to apply a class to the <li> tag that encloses an anchor tag when a user clicks on the link. Like this:

<ul>
<li><a href="#">Just an Example</a></li>
</ul>

So when the user clicks on <a href="#">Just an Example</a>, I need to apply a class to the <li> enclosing it. How would I target this? I am using jquery.

+2  A: 
$('a[href=#]').click(function(){
   $(this).closest('li').addClass('your_class_name');
});

If its guaranteed that the parent element of such an anchor is a LI you might also use

$(this).parent().addClass('your_class_name');
jAndy
+1  A: 
$("a").click(function() {
   $(this).parent().whateveryouwant();
}
+1  A: 

just use

$('a').click(function(){
    $(this).parent().addClass('theClass');
});
rob waminal