Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Select 2 Divs Up

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!

like image 651
Dodinas Avatar asked Jun 08 '26 13:06

Dodinas


1 Answers

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.

like image 104
dcharles Avatar answered Jun 11 '26 23:06

dcharles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!