Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid .prev().prev().prev() OR how to look up an item with a class before the current one

Tags:

jquery

I looked up the whole jQuery Docs, but i doesn't found anything: Is it possible to search an element before the current one with a specific class? I thought this will work, but it doesn't:

$(this).prev('.bar');

http://jsfiddle.net/Ejh5G/

like image 421
Slevin Avatar asked Jan 27 '26 04:01

Slevin


2 Answers

Try:

 $('.d').prevAll('.a').css('color','red');

Fiddle

To address Jan's point, you can do this:

$('.d').prevAll('.a').first().css('color','red');
like image 97
Matt Burland Avatar answered Jan 30 '26 18:01

Matt Burland


Use .prevAll() :

$(this).prevAll('.bar');// all elements    
$(this).prevAll('.bar').eq(0); //first element
$(this).prevAll('.bar:last'); //last element
like image 40
A. Wolff Avatar answered Jan 30 '26 16:01

A. Wolff



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!