forked from escueladigital/EDui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
32 lines (28 loc) · 1.04 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Crear elementos con atributos e hijo
const createCustomElement = (element,attributes,child) => {
let customElement = document.createElement(element);
if (child !== null) {
child.nodeType === 1 || child.nodeType === 11 ? customElement.appendChild(child) : customElement.innerHTML = child;
}
addAttributes(customElement,attributes);
return customElement;
};
// Añadir un objeto de atributos a un elemento
const addAttributes = (element, attrObj) => {
for (let attr in attrObj) {
if (attrObj.hasOwnProperty(attr)) element.setAttribute(attr,attrObj[attr])
}
};
// Envolver un elemento con otro
const wrap = (selector, wrapElementType, attributesObj) => {
const element = document.querySelector(selector);
if (element) {
const nextSibling = element.nextElementSibling,
parent = element.parentElement,
wrapElement = createCustomElement(wrapElementType,attributesObj,element);
nextSibling
? parent.insertBefore(wrapElement,nextSibling)
: parent.appendChild(wrapElement);
return wrapElement;
}
};