Guest User

Untitled

a guest
Dec 10th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. var $ = function(s){ // Makes the main function (accepts one value)
  2. var o = []// Makes an empty array
  3. var sel = s.substring(1,s.length) // Get's the selector text minus the first character (like #)
  4. switch (s.charAt(0)){ // Switch based on first chararcter
  5. case '#': // If it's a #, do document.getElementById
  6. o.push(document.getElementById(sel))// the element is pushed to o instead of the element being returned
  7. break;
  8. case '.': // If it's a ., get the element by class name
  9. o.push(document.getElementsByClassName(sel))
  10. break;
  11. case '*': // If a star, do document.getElementByTagName
  12. o.push(document.getElementsByTagName(sel))
  13. break;
  14. }
  15. for (var i in o){ //Gives all the nodes inside o the on method
  16. o[i].on = function(type, callback)
  17. {this.addEventListener(type, callback);return this}
  18. }
  19. if (o.length === 1){ // If o only has 1 element
  20. return o[0] // Returns the element
  21. }
  22. else {
  23. return o // Returns element list
  24. }
  25. }
Add Comment
Please, Sign In to add comment