Lucas Costa Pauli
Lucas Costa Pauli

Reputation: 3

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

Upvotes: 0

Views: 37

Answers (1)

Iłya Bursov
Iłya Bursov

Reputation: 24156

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>

Upvotes: 1

Related Questions