Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //this code does not rely on Jquery-Replacement-Utility-Code-main-code.js
- var getElement = function(selector) { // or var $ = function(selector) {
- var wrapper = document.querySelector(selector);
- // get Dom wrapper.
- //function to add custom functions to the wrapper.
- this.addFunction = function(name, code) {
- // if code is undefined, assume name is an object with name:function pairs
- //enumerate the object name
- if (!code) {
- for (var key in name) {
- //make sure item is not a prototype
- if (name.hasOwnProperty(key)) {
- var value = name[key];
- //set function name in the wrapper to the value(function code)
- wrapper[key] = value;
- }
- }
- }
- //there is only one function being defined. Set the name to the function(code)
- else {
- wrapper[name] = code;
- }
- };
- //add utility functions here
- this.addFunction({
- //html and addClass was added as an example
- //when referring to the element, use `this` instead
- "html": function(value) {
- if (value) {
- //there is an value, so we just update the value of the element
- this.innerHTML = value;
- }
- else {
- //value is not defined, so we return the innerHTML of the element
- return this.innerHTML;
- }
- },
- "addClass": function(className) {
- if (el.classList)
- this.classList.add(className);
- else
- this.className = this.className + ' ' + className;
- }
- });
- return wrapper;
- };
- //actual function usage
- //get our element
- var element = getElement("#someElementId");
- //set the value to Hello, World!
- element.html("Hello, World");
- //add the class hello-world
- element.addClass("hello-world");
- //or we could do this:
- //get the value of the element
- var elementHTML = getElement("#someElementId").html();
- console.log(elementHTML);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement