Extend Prototype with more DOM functions
Login or Register to Bookmark this snippet
Extend the Prototype JS API by adding this afterwards.
This will give your DOM Elements 3 more functions:
- searchUp
elm.searchUp('table');
Will return the DOM element that is the first ancestor up the DOM tree. - removeChildren
elm.removeChildren();
Will remove all DOM nodes from the element. - destroy
elm.destroy();
Will remove itself from the DOM tree.
var MyUtils = {
// searchUp - searches the dom tree upwards for the findElm node starting from elm.
// @author jcurnow
searchUp: function (element, find_node_name) {
element = $(element);
while (element && element.parentNode && element.nodeName.toLowerCase() != find_node_name && element.nodeName.toLowerCase() != 'body') {
element = element.parentNode;
}
return element;
},
// removeChildren - removes all dom childs
// @author jcurnow
removeChildren: function(element) {
element = $(element);
while (element.hasChildNodes()) {
element.removeChild(element.firstChild);
}
return true;
},
// destroy - Removes the element from the dom
// @author jcurnow
destroy: function(element){
element = $(element);
if (element && element.parentNode.removeChild(element)){
return true;
}
return false;
}
}
Element.addMethods(MyUtils);
Added by JC on 30th December, 2007
There are no comments about this snippet.
You must be registered and logged in to post a comment.
Login here to post a comment