Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can anyone tell me why this script.js isn't working?

Can anyone tell me why this isn’t working? the intent is to change the title when clicked to a random rgb color (the onclick function doesn’t even appear on my gitpage: https://lucaspaulii.github.io/portfolio/)

const title = document.getElementById("title");

let randNum = () => {
    let num = Math.floor(Math.random() * 255);
    return num
}

let randColor = () => {
    return 'rgb('+ randNum() + ',' + randNum() + ',' + randNum() + ')'
}

let newColor = randColor();

title.addEventListener('click', function onClick() {
    title.style.color = newColor; 
})

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

you’ calling randColor only one time, call it inside of handler:

const title = document.getElementById("title");

let randNum = () => {
    let num = Math.floor(Math.random() * 255);
    return num
}

let randColor = () => {
    return 'rgb('+ randNum() + ',' + randNum() + ',' + randNum() + ')'
}

title.addEventListener('click', function onClick() {
    title.style.color = randColor(); 
})
<div id="title">click here</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading