const array = [ 1, [ 2, [ 3, [ 4, [ 5 ] ] ] ] ]
const flattenArray = (array) => {
return array.reduce((res, it) => {
return res.concat(Array.isArray(it) ? flattenArray(it) : it)
}, [])
}
console.log(flattenArray(array)) // [1, 2, 3, 4, 5]
const prizes = [ 'A', 'B', 'C', 'D' ]
prizes.sort(() => 0.5 - Math.random())
const lis = [...document.querySelectorAll("#wizard > li")];
lis.forEach((li) => {
li.addEventListener("click", () => {
const index = lis.indexOf(li);
console.log(index);
});
});
// https://thewebdev.info/2022/04/22/how-to-get-the-bodys-content-of-an-iframe-in-javascript/
const iframe = document.querySelector("#id_description_iframe");
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
const iframeContent = iframeDocument.querySelectorAll("#frameBody");
function addCSSRule(sheet, selector, rules, index) {
if(sheet.insertRule) {
sheet.insertRule(selector + "{" + rules + "}", index);
}
else {
sheet.addRule(selector, rules, index);
}
}
// Use it!
addCSSRule(document.styleSheets[0], "header", "float: left");
// Getting a CSS Variable's Value
getComputedStyle(document.documentElement) // or specific element
.getPropertyValue('--my-variable-name'); // #999999
// Setting a CSS Variable's Value
document.documentElement.style // or specific element
.setProperty('--my-variable-name', 'pink');
// OR
var root = document.querySelector(':root'); // = document.documentElement
<body>
<input type="color" id="color" value="#c1e0ff" />
</body>
<script>
const color = document.querySelector('#color')
const div = document.body
color.addEventListener('input', () => {
div.style.backgroundColor = color.value
})
color.dispatchEvent(new Event('input'));
</script>
var Wrapper = (function () {
this.A = () => {
console.log('A');
}
this.B = () => {
console.log('B');
}
return {
A2: this.A,
B2: this.B
}
// or return this
})();
Wrapper.B2(); // B
const e = new Event('change')
const element = document.querySelector('#' + this.fakeFileId)
element.dispatchEvent(e)