Extend Prototype with more DOM functions

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

Added by JC on 31st December 2007

Comments

There are no comments about this snippet.

Post Comment Github Markdown Supported

You must be logged in to post a comment.

Login here to post a comment