Creating jQuery.prev (). Prev (). Prev (). Find ("selector") calls

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

+8
jquery
source share
4 answers

You can use .prevAll in combination with eq or :eq , since prevAll returns the set of elements in the reverse order, starting from the element closest to the current element [0] , equivalent to .prev().prev().prev() will be .prevAll().eq(2) or .prevAll(':eq(2)') .

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

+21
source share

You can do prevAll() with a selector.

 $('div').prevAll('a:last') 
+5
source share

Try closest , which searches for the closest ancestor that matches the selector. eg.

 $(this).closest('table').find('selector') 

Edit: Added find () function at the end

+2
source share

take a look at nth-child in jquery, may be useful ...

-one
source share

All Articles