February 2007

You must know the $ Function in prototype (also in jQuery and mootools) that helps you target elements in Javascript. Well, here's my take on it.

Here's the code :

  1. function $( magic , node ){
  2.       //model : [@][id][:][tag]
  3.  
  4.       var keys = /^([\@]*)([\w+]*)([\:]*)([\w+]*)$/(magic);
  5.       var isArray = keys[1];
  6.       var id = keys[2];
  7.       var tag = keys[4];
  8.  
  9.       if( tag == "" ){tag = "*"}
  10.       if( node == undefined ){ node = document }
  11.  
  12.       var nodes = node.getElementsByTagName(tag);
  13.       var ret = new Array;
  14.       for( var i=0; i < nodes.length; i++ ){