Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making jQuery's .prev().prev().prev().find("selector") calls easier

Tags:

jquery

How can I avoid the .prev().prev().prev() calls? Is there a shortcut for this?

like image 902
bubulina Avatar asked Feb 07 '11 12:02

bubulina


1 Answers

You can use .prevAll, combined with eq or :eq, since prevAll returns a set of element in the reverse order starting with the element that's closest to the current element being [0], the equivalent to .prev().prev().prev() would be .prevAll().eq(2) or .prevAll(':eq(2)').

See this simple demo: http://www.jsfiddle.net/QZYHN/

like image 118
Yi Jiang Avatar answered Nov 16 '22 01:11

Yi Jiang