Rob Kinyon - DOM.Utils-0.02

Documentation | Source

NAME

DOM.Utils

DESCRIPTION

This provides a group of useful functions for use within the DOM.

DEPENDENCIES

This requires JSAN to be installed.

FUNCTIONS

These are functions that are exported to the JSAN.use()er's namespace.

$()

This function will attempt, if given a string, to find an element in the DOM that corresponds to that string. If given anything else, it will return that back.

It will attempt to call the following methods, in order:

  • document.getElementById( arg )
  • document.getElementsByName( arg )[0]
  • document.getElementsByClass( arg )[0]
  • document.getElementsByTag( arg )[0]

In the case where a method returns back a collection, the first one from that collection will be chosen.

  function someFunction ( element, ... ) {
      // Guarantee that element is actually an element object
      element = $(element);
      ...
  }

ADDITIONS TO document

getElementsByClass()

This method acts as getElementsByName(), but checks against the classes vs. the name.

getElementsByClassName() is an alias that is provided for Prototype API compatibility.

SUPPORT

Currently, there is no mailing list or IRC channel. Please send bug reports and patches to the author.

AUTHOR

Rob Kinyon (rob.kinyon@iinteractive.com)

Originally written by Sam Stephenson (sam@conio.net)

My time is generously donated by Infinity Interactive, Inc. http://www.iinteractive.com

/*

*/

if ( typeof( DOM ) == 'undefined' ) {
    DOM = {};
}

/*

*/

DOM.Utils = {
    EXPORT: [ '$' ]
   ,'$' : function () {
        var elements = new Array();

        for (var i = 0; i < arguments.length; i++) {
            var element = arguments[i];

            if (typeof element == 'string')
                element = document.getElementById(element)
                    || document.getElementsByName(element)[0]
//                    || document.getElementsByTagName(element)[0]
                    || undefined
                ;

            if (arguments.length == 1) 
                return element;

            elements.push( element );
        }

        return elements;
    }
};

/*

*/

document.getElementsByClass = function(className) {
    var children = document.getElementsByTagName('*') || document.all;
    var elements = new Array();
  
    for (var i = 0; i < children.length; i++) {
        var child = children[i];
        var classNames = child.className.split(' ');
        for (var j = 0; j < classNames.length; j++) {
            if (classNames[j] == className) {
              elements.push(child);
              break;
            }
        }
    }
  
    return elements;
};
document.getElementsByClassName = document.getElementsByClass;

/*

*/