Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- }
Add Comment
Please, Sign In to add comment