views:

165

answers:

2

Hi,

A click event occurs on a href tag, and I need to reference the class of a that is 9 levels up,

so:

<div class="blah" id="234">
 <div>
     <div>
        <div>
          <div>
            <div>
             <div><a href=""></a>

The only way I know is to call parent() 9 times, what else can I do?

+7  A: 

You can supply a selector via the parents function

$(a).click(function() {
   var theIdIs = $(this).parents("div.blah").attr("id");
});
sighohwell
And you can either do div.blah:first or .slice(0,1) to just get the closest one. (E.g if you want the closest div and there are more divs outside.)
svinto
ooh didn't know about slice, nice!
Andrew Bullock
+2  A: 

Also if your using jQuery 1.3+ you can use the closest method

$('a').click(function() {
   if ($(this).closest("div.blah").hasClass("blah"))
   {
      // Do something with it
   }
});
bendewey