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<
I've created a new jQuery plugin which returns the "real" previous input element. Include this code:
(function($){
$.fn.realPrev = function(selector){
var thisElem = this.get(0); //Get DOM element for comparison
var lastElement, foundElement = null;
$(selector).each(function(){
if(this == thisElem){
foundElement = lastElement;
return false;
}
lastElement = this;
});
return $(foundElement);
}
})(jQuery);
$("#input3").realPrev("input");
Fiddle: http://jsfiddle.net/QWRWh/