Snippets v2

Languages
My Snippets

Extend Prototype with more DOM functions Login or Register to Bookmark this snippet

Description Javascript

Extend the Prototype JS API by adding this afterwards.

This will give your DOM Elements 3 more functions:

The Code Download
  1. var MyUtils = {
  2.  
  3.     // searchUp - searches the dom tree upwards for the findElm node starting from elm.
  4.     // @author jcurnow
  5.     searchUp: function (element, find_node_name) {
  6.         element = $(element);
  7.         while (element && element.parentNode && element.nodeName.toLowerCase() != find_node_name && element.nodeName.toLowerCase() != 'body') {
  8.             element = element.parentNode;
  9.         }
  10.         return element;
  11.     },
  12.  
  13.     // removeChildren - removes all dom childs
  14.     // @author jcurnow
  15.     removeChildren: function(element) {
  16.         element = $(element);
  17.         while (element.hasChildNodes()) {
  18.             element.removeChild(element.firstChild);
  19.         }
  20.         return true;
  21.     },
  22.  
  23.     // destroy - Removes the element from the dom
  24.     // @author jcurnow
  25.     destroy: function(element){
  26.         element = $(element);
  27.         if (element && element.parentNode.removeChild(element)){
  28.             return true;
  29.         }
  30.         return false;
  31.     }
  32.  
  33. }
  34. Element.addMethods(MyUtils);
Credits Contact JC

Added by JC on 30th December, 2007

Comments

There are no comments about this snippet.

Post Comment HTML is allowed

You must be registered and logged in to post a comment.

Login here to post a comment