Skip to content

Instantly share code, notes, and snippets.

@jaeschrich
Created August 25, 2012 21:02
Show Gist options
  • Save jaeschrich/3470957 to your computer and use it in GitHub Desktop.
Save jaeschrich/3470957 to your computer and use it in GitHub Desktop.
Nano.js. A 25-line jQuery impersonator. Incomplete version
var $ = function(s){ // Makes the main function (accepts one value)
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
return document.getElementById(sel)
break;
case '.': // If it's a ., get the element by class name
var allEls = document.getElementsByTagName('*')
for (var i = 0;i<allEls.length;i++){
if (allEls[i].className === sel){
return allEls[i]
}
}
break;
case '*': // If a star, do document.getElementByTagName
return document.getElementsByTagName(sel)
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment