arteta
arteta

Reputation: 33

How to get the li just before a <a tag> using jquery

am stuck with a issue.juz started studying jquery recently..i do hav a issue

i have a segment like this in ma html code..what i need is to select the li tag that comes just before the 'a' tag that i clicked.it would b helpful if someone helped me with this. i need to add a class named open in the li class just before the 'a' tag that is being clicked.. how can i do this using jquery..

<ul>
 <li class=''>
       <a class ='value1'></a>
           <div>some contents</div>
 </li>

 <li class=''>
       <a class ='value 2'></a>
           <div>some contents</div>
 </li>

</ul>

Upvotes: 2

Views: 97

Answers (1)

Darren
Darren

Reputation: 70718

Try:

$("a").click(function() {
   $(this).parent().addClass("open");
});

http://jsfiddle.net/4hGvS/

I have added some content to your a tag for this to work in JSFiddle.

Upvotes: 1

Related Questions