+ 1
Why can't it read style?
Sorry the code is so messy. https://code.sololearn.com/W3X3g8VHjncr/?ref=app
4 Answers
+ 3
Now it need to put peak() function in window.onload=()=>{..
function peak() {
..
}
peak() ;
};
+ 2
Lego in Motion
Replace peak function with this code:
function peak() {
var moles = Math.floor(Math.random() * 9 + 1);
var ele = document.getElementById("m" + moles);
if (ele != null) {
ele.style.backgroundColor = "#600";
}
}
+ 2
// I Hope this link helps you
https://code.sololearn.com/WvmFddRdzqGB
let ms = document.querySelectorAll(".mole");
window.onload = () => {
ms.forEach((element, index) => {
element.addEventListener("click", WackOnClick);
});
peak();
}
function WackOnClick() {
this.style.backgroundColor = "#600";
}
function peak() {
let p = Math.floor(Math.random()*9+1);
let pElem = document.getElementById("m"+p);
if(pElem != null) {
pElem.style.backgroundColor = "#600";
}
}
+ 1
If it's not an inline style, you only can read style by getComputedStyle method
const element = document.querySelector('.mole')
const style = getComputedStyle(element)