SHARE
TWEET

Dom Module

a guest May 1st, 2013 142 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var domModule = (function () {
  2.     var buffer = [];
  3.     function appendChild(elementToAdd, selector) {
  4.         var parentElement = document.querySelector(selector);
  5.         var newElement = document.createElement(elementToAdd);
  6.         parentElement.appendChild(newElement);
  7.     }
  8.     function removeChild(parentSelector, removeSelector) {
  9.         var parentsList = document.querySelectorAll(parentSelector);
  10.         for (var i = 0; i < parentsList.length; i++) {
  11.             var elementToRemove = parentsList[i].querySelector(removeSelector);
  12.             parentsList[i].removeChild(elementToRemove);
  13.         }
  14.     }
  15.     function addHandler(elementSelector, eventType, callback) {
  16.         var elements = document.querySelectorAll(elementSelector);
  17.         for (var i = 0; i < elements.length; i++) {
  18.             elements[i].addEventListener(eventType, callback,false);
  19.         }
  20.     }
  21.     function bufferElement(parentElement, ellementToAdd) {
  22.         this.parentElement = parentElement;
  23.         this.ellementToAdd = ellementToAdd;
  24.     }
  25.     function appendBufferToDom() {
  26.         for (var i = 0; i < buffer.length; i++) {
  27.             buffer[i].parentElement.appendChild(buffer[i].ellementToAdd);
  28.         }
  29.         buffer = [];
  30.     }
  31.     function appendToBuffer(parentSelector, newElement) {
  32.         var parentElement = document.querySelector(parentSelector);
  33.         var elementToAdd = document.createElement(newElement);
  34.         buffer.push(new bufferElement(parentElement, elementToAdd));
  35.         if (buffer.length >= 100) {
  36.             appendBufferToDom();
  37.         }
  38.     }
  39.  
  40.  
  41.     return {
  42.         appendChild: appendChild,
  43.         removeChild: removeChild,
  44.         addHandler: addHandler,
  45.         appendToBuffer: appendToBuffer
  46.     };
  47. })();
  48.  
  49. (function () {
  50.     var test = function test() {
  51.         alert("You click!");
  52.     }
  53.  
  54.     domModule.appendChild("div", "body");
  55.     alert("Div is on screen!");
  56.     domModule.removeChild("body", "div");
  57.     alert("Div is removed!");
  58.     domModule.addHandler("a", "click", test);
  59.     for (var i = 0; i < 101; i++) {
  60.         domModule.appendToBuffer("body", "div");
  61.     }
  62.     alert("100 divs added!");
  63. }());
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top