Skip to content

Instantly share code, notes, and snippets.

@jaeschrich
Created August 25, 2012 20:11
Show Gist options
  • Save jaeschrich/3470445 to your computer and use it in GitHub Desktop.
Save jaeschrich/3470445 to your computer and use it in GitHub Desktop.
Nano.js. A 25-line jQuery impersonator. Complete Version
var $ = function(s){ // Makes the main function (accepts one value)
var o = []// Makes an empty array
var sel = s.substring(1,s.length) // Get's the selector text minus the first character (like #)
switch (s.charAt(0)){ // Switch based on first chararcter
case '#': // If it's a #, do document.getElementById
o.push(document.getElementById(sel))// the element is pushed to o instead of the element being returned
break;
case '.': // If it's a ., get the element by class name
o.push(document.getElementsByClassName(sel))
break;
case '*': // If a star, do document.getElementByTagName
o.push(document.getElementsByTagName(sel))
break;
}
for (var i in o){ //Gives all the nodes inside o the on method
o[i].on = function(type, callback)
{this.addEventListener(type, callback);return this}
}
if (o.length === 1){ // If o only has 1 element
return o[0] // Returns the element
}
else {
return o // Returns element list
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment