Guest User

Untitled

a guest
Jul 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. var DomFactory = (function (document) {
  2.  
  3. var api = {
  4. element: function (name, attributes) {
  5. var el = document.createElement(name);
  6.  
  7. if (attributes) {
  8. for (var key in attributes) {
  9. if (attributes.hasOwnProperty(key)) {
  10. el.setAttribute(key, attributes[key]);
  11. }
  12. }
  13. }
  14.  
  15. return el;
  16. },
  17.  
  18. div: function (attributes) {
  19. return api.element('div', attributes);
  20. }
  21. };
  22.  
  23. return api;
  24.  
  25. }(window.document));
  26.  
  27. function attach(source, target, position) {
  28. switch (position) {
  29. case 'before': {
  30. target.parentNode.insertBefore(source, target);
  31. break;
  32. }
  33. case 'after': {
  34. if (target.nextSibling) {
  35. target.parentNode.insertBefore(source, target.nextSibling);
  36. } else {
  37. target.parentNode.appendChild(source);
  38. }
  39. }
  40. }
  41. }
  42.  
  43. /*
  44. var div = DomFactory.div({ 'class': 'hero' });
  45. var table = DomFactory.element('table', { 'class': 'table table-bordered' });
  46. attach(table, div, 'before');
  47. */
Add Comment
Please, Sign In to add comment