Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function createElement(options) {
- let el;
- if (!options.tagName) {
- el = document.createDocumentFragment();
- } else {
- el = document.createElement(options.tagName);
- if (options.className) {
- el.className = options.className;
- }
- if (options.attributes) {
- for (let a in options.attributes) {
- el.setAttribute(a, options.attributes[a]);
- }
- }
- if (options.on) {
- for (var e in options.on) {
- el.addEventListener(e, options.on[e]);
- }
- }
- if (options.style) {
- for (let s in options.style) {
- elem.style[s] = options.style[s];
- }
- }
- if (options.innerHTML) {
- el.innerHTML = options.innerHTML;
- }
- }
- if (options.text) {
- el.appendChild(document.createTextNode(options.text));
- }
- // IE 8 doesn't have HTMLElement
- if (window.HTMLElement === undefined) {
- window.HTMLElement = Element;
- }
- if (options.childs && options.childs.length) {
- let child;
- for (let i = 0; i < options.childs.length; i++) {
- child = createElement(options.childs[i]);
- if (child instanceof window.HTMLElement) {
- el.appendChild(child);
- }
- }
- }
- return el;
- }
- module.exports.default = createElement;
Add Comment
Please, Sign In to add comment