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

Issue

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

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>

Answered By – Iłya Bursov

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published