Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. //this code does not rely on Jquery-Replacement-Utility-Code-main-code.js
  2.  
  3. var getElement = function(selector) { // or var $ = function(selector) {
  4. var wrapper = document.querySelector(selector);
  5.  
  6. // get Dom wrapper.
  7. //function to add custom functions to the wrapper.
  8. this.addFunction = function(name, code) {
  9. // if code is undefined, assume name is an object with name:function pairs
  10. //enumerate the object name
  11. if (!code) {
  12.  
  13. for (var key in name) {
  14. //make sure item is not a prototype
  15. if (name.hasOwnProperty(key)) {
  16.  
  17. var value = name[key];
  18. //set function name in the wrapper to the value(function code)
  19. wrapper[key] = value;
  20.  
  21. }
  22. }
  23. }
  24. //there is only one function being defined. Set the name to the function(code)
  25. else {
  26. wrapper[name] = code;
  27. }
  28. };
  29. //add utility functions here
  30. this.addFunction({
  31. //html and addClass was added as an example
  32. //when referring to the element, use `this` instead
  33. "html": function(value) {
  34. if (value) {
  35. //there is an value, so we just update the value of the element
  36. this.innerHTML = value;
  37. }
  38. else {
  39. //value is not defined, so we return the innerHTML of the element
  40. return this.innerHTML;
  41.  
  42. }
  43. },
  44. "addClass": function(className) {
  45. if (el.classList)
  46. this.classList.add(className);
  47. else
  48. this.className = this.className + ' ' + className;
  49. }
  50.  
  51.  
  52. });
  53.  
  54.  
  55. return wrapper;
  56.  
  57. };
  58. //actual function usage
  59. //get our element
  60. var element = getElement("#someElementId");
  61. //set the value to Hello, World!
  62. element.html("Hello, World");
  63. //add the class hello-world
  64. element.addClass("hello-world");
  65. //or we could do this:
  66. //get the value of the element
  67. var elementHTML = getElement("#someElementId").html();
  68. console.log(elementHTML);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement