Guest User

Untitled

a guest
Apr 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. function createElement(options) {
  2. let el;
  3. if (!options.tagName) {
  4. el = document.createDocumentFragment();
  5. } else {
  6. el = document.createElement(options.tagName);
  7. if (options.className) {
  8. el.className = options.className;
  9. }
  10. if (options.attributes) {
  11. for (let a in options.attributes) {
  12. el.setAttribute(a, options.attributes[a]);
  13. }
  14. }
  15. if (options.on) {
  16. for (var e in options.on) {
  17. el.addEventListener(e, options.on[e]);
  18. }
  19. }
  20. if (options.style) {
  21. for (let s in options.style) {
  22. elem.style[s] = options.style[s];
  23. }
  24. }
  25. if (options.innerHTML) {
  26. el.innerHTML = options.innerHTML;
  27. }
  28. }
  29. if (options.text) {
  30. el.appendChild(document.createTextNode(options.text));
  31. }
  32. // IE 8 doesn't have HTMLElement
  33. if (window.HTMLElement === undefined) {
  34. window.HTMLElement = Element;
  35. }
  36. if (options.childs && options.childs.length) {
  37. let child;
  38. for (let i = 0; i < options.childs.length; i++) {
  39. child = createElement(options.childs[i]);
  40. if (child instanceof window.HTMLElement) {
  41. el.appendChild(child);
  42. }
  43. }
  44. }
  45. return el;
  46. }
  47.  
  48. module.exports.default = createElement;
Add Comment
Please, Sign In to add comment