tags:

views:

41

answers:

3

I wrote function onclick of a element

   <div class="left_collapsed">Որոնում</div>
        <div class="container" style="display: none;">
           <ul> 
              <li>
                 <a href="" id="search_refresh" >Թարմացնել համակարգը</a>
              </li>
           </ul>
        </div>
   </div>

$("#search_refresh").click(function(u)
{
     how can i achieve to first div element here?
     (i have many div's with left_collapsed class )

});

i wrote $(this).parent("li").parent("ul").parent("div").before("div").addClass("left_expended");

but i dislike it:)

Thanks much

A: 

Try jQuery.parents, something like this:

$("#search_refresh").click(function()
{
     var element = $(this).parents(".left_collapsed");
});

Update: jAndy's answer is better, you should use closest instead, particularly if the left_collapsed divs are nested such that #search_refresh is inside multiple left_collapsed divs.

Douglas
There is one `(` too much.
Willi
as i mentioned, i have many elements with `class="left_collapsed"`. i need exacltly that div.
Syom
Are those `.left_collapsed` elements nested inside each other?
Willi
yes, in some cases.
Syom
Can't you just assign another (special) class to that particular `.left_collapsed` element?
Willi
A: 
$(this).parents('.left_collapsed');
Willi
see the comment.
Syom
+6  A: 

You can either use .closest()

$(this).closest('.left_collapsed');

or .parents()

$(this).parents('.left_collapsed');

Actually, .closest() is a better choice, since it'll only grab parent nodes until it reaches the desired one, whereass .parents() grabs all parent nodes (up to the document root).

Ref.: .parents(), .closest()

jAndy
perfect. Thanks much:/
Syom