is there a simple way to get the previous occurrence of an element in the DOM? If I\'m looking at #text3
and I want to get ahold of the previous input #text2<
Both other answers assume that the element you are using is of the same type of the element you're looking for. If it's arbitrary, this should work (performance not be great on larger files).
Sort of combining the two other answers:
(function($){
$.fn.realPrev = function(selector){
var all = $("*");
return all.slice(0,all.index(this)).filter(selector).last();
}
})(jQuery);
http://jsfiddle.net/QWRWh/1/