Hi folks,
I'm very new to jQuery and am playing around with what is possible.
I found myself wanting a findParent function and couldn't spot one
ready made. I'd love it if people could give me feedback on my
implementation. I'm sure there's plenty I could do better. If this
function also exists I'd like to know where it is too. =)
/**
* return the first parent that matches the selector. Optionally
include the src element in the search
*/
$.fn.findParent = function()
{
var _this, _selector, _includeSelf, _result;
function find(index, elem)
{
if (!_includeSelf) elem = elem.parentNode;
while (elem)
{
if ($(elem).is(_selector)) return _result.push(elem);
elem = elem.parentNode;
}
}
function _findParent(selector, includeSelf)
{
var thisp = _this = this;
_selector = selector;
_includeSelf = includeSelf;
_result = [];
this.each(find);
var retObj = $(_result);
retObj.end = function () { return thisp };
return retObj;
}
return _findParent;
}();
Cheers,
Pete