PDA

View Full Version : Function suggestions



TommyMaintz
29 Dec 2006, 11:15 AM
Hey,

Could you consider adding these 2 functions to the library?
I use them all the time and i think they can come in really handy.

This one is to get an elements first parent that has an id. You can give a specific tag you want to search for.


YAHOO.ext.Element.prototype.getFirstParentWithId = function(tag) {
var tag = tag || false;
var checkNode = YAHOO.util.Dom.get(this.id).parentNode;
while(!checkNode.id || (tag !== false && checkNode.tagName != tag)) {
checkNode = checkNode.parentNode;
if(checkNode.tagName === 'body') {
return this;
}
}
return getEl(checkNode);
};
This one is to strip the prefix from element id's. you can give a delimiter to specify what to strip.


YAHOO.ext.Element.prototype.stripId = function(delimiter) {
var delimiter = delimiter || '-';
var id = this.id;
return id.substr(id.lastIndexOf(delimiter)+1);
};
Example where i use both functions at once:


var clickedEl = $(e.findTarget());
var dlg = .....
dlg.show($('website-' + clickedEl.getFirstParentWithId().stripId()))