Why can't it read style? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why can't it read style?

Sorry the code is so messy. https://code.sololearn.com/W3X3g8VHjncr/?ref=app

27th Mar 2022, 2:06 PM
Lego in Motion
Lego in Motion - avatar
4 Answers
+ 3
Now it need to put peak() function in window.onload=()=>{.. function peak() { .. } peak() ; };
27th Mar 2022, 2:17 PM
Jayakrishna 🇮🇳
+ 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"; } }
27th Mar 2022, 2:22 PM
A͢J
A͢J - avatar
+ 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"; } }
27th Mar 2022, 3:10 PM
SoloProg
SoloProg - avatar
+ 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)
28th Mar 2022, 5:39 AM
Calviղ
Calviղ - avatar