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

Time:05-21

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; 
})

CodePudding user response:

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>

  • Related