views:

155

answers:

3

Hi All,

I'm a bit confused on how to properly select a parent item, that is 2 DIV's up.

In other words, my HTML looks like this:

 <div>Folder</div>
 <div class="file_info">
   <a href="test.jpg">Test File</a>
 </div>

Here's my current jQuery: $("a[href$='.jpg']").addClass("Image");

However, I don't want to add the class to the <a>, instead, I want to add the class to the 'folder' 2 DIV's above.

What's the best way to do this?

Thanks!

A: 
$("a[href$='.jpg']").parent().parent().addClass('Image');
ChaosPandion
+1  A: 
$("a").parent().parent();

or

$("a").parents("div:eq(1)");

or

$("a").parents("div").get(1);

etc...

Jonathan Sampson
+2  A: 

This will work:

$("a[href$='.jpg']").parent().prev().addClass("Image");

First it navigates to the parent element of the selected element and then to the nearest sibling before that element, thus reaching the div element you require.

See the documentation on parent and prev for more details.

Marve