Free javascript Hosting


dom-library.js

Uploaded on Apr 22 2022 23:22 by dom-prog

Element.prototype.set = function CustomSet(...atrs) {
let atrsObj = {};
if (!(atrs[0] instanceof Object))
/*if NOT obj*/
atrs.forEach((atr, i, arr) =>
i % 2 == 0 ? (atrsObj[atr] = arr[i + 1]) : void 0
);
else atrsObj = atrs[0]; /*isObj*/
for (let atr in atrsObj) {
let dots = atr.split(".");
let temp = this;
for (let i = 0; i < dots.length - 1; i++) temp = temp[dots[i]];
temp[dots.slice(-1)[0]] = atrsObj[atr];
}
return this;
};
Element.prototype.setStyle = function CustomSetStyle(...atrs) {
let atrsObj = {};
if (!(atrs[0] instanceof Object))
/*if NOT obj*/
atrs.forEach((atr, i, arr) =>
i % 2 == 0 ? (atrsObj[atr] = arr[i + 1]) : void 0
);
else atrsObj = atrs[0]; /*isObj*/
for (let atr in atrsObj) {
let dots = atr.split(".");
let temp = this.style;
for (let i = 0; i < dots.length - 1; i++) temp = temp[dots[i]];
temp[dots.slice(-1)[0]] = atrsObj[atr];
}
return this;
};
var createElement = function CustomCreateElement(
tag,
append,
appendType = "id",
appendIndex = 0
) {
/*("Tag"||["Tag",...]), Element||"","id/class/name/tag/",# */
if (!append) {
let elems = [];
for (tag of tag instanceof Array ? tag : [tag])
elems.push(document.createElement(tag));
return elems.length > 1 ? elems : elems[0];
}
var isElement = (elem) =>
elem instanceof Element || elem instanceof HTMLDocument;
if (!isElement(append) && typeof append == "string") {
if (appendType === "id") append = document.getElementById(append);
if (appendType === "class")
append = document.getElementsByClassName(append)[appendIndex];
if (appendType === "name")
append = document.getElementsByName(append)[appendIndex];
if (appendType === "tag")
append = document.getElementsByTagName(append)[appendIndex];
}
if (!isElement(append))
console.warn(
((e = new Error()), (e.name += ": No Element Found"), e.stack)
);
let elems = [];
for (tag of tag instanceof Array ? tag : [tag]) {
elems.push(append.appendChild(document.createElement(tag)));
}
return elems.length > 1 ? elems : elems[0];
};

Back to list