I needed to find the nearest element, relative to another element. I wanted a generic function not locked to a spesific tree structure. Maybe it already exists within jQuery and if so please show me! Here is what I came up with and it works for what I needed:
$.fn.nearest = function(s) {
var o = {};
var p = $(this).parent();
while(p.length) {
if(p.find(s).length) {
o = p.find(s).first();
break;
}
else {
p = p.parent();
}
}
return o;
};
-Chris
o = $();
would be better for compatibility. – Mailer