Error on changing backgroundColor with javascript
I am using this code:
function myTimer() {
const random_hex_color_code = () => {
let n = (Math.random() * 0xfffff * 1000000).toString(16);
return '#' + n.slice(0, 6);
};
console.log(random_hex_color_code())
this.Sym1.on("added", function () {
document.getElementById("Sym1").style.backgroundColor = (random_hex_color_code());
});
}
setInterval(myTimer, 3000);Error message: "Uncaught TypeError: Cannot read property 'on' of undefined
at myTimer" - and no change of backgroundColor happens
if I use a simplified version like:
function myTimer() {
const random_hex_color_code = () => {
let n = (Math.random() * 0xfffff * 1000000).toString(16);
return '#' + n.slice(0, 6);
};
console.log(random_hex_color_code())
document.getElementById("Sym1").style.backgroundColor = (random_hex_color_code());
}
setInterval(myTimer, 3000);Error message: Uncaught TypeError: Cannot read property 'style' of null
at myTimer - and no change of background-color happens
any ideas on correct syntax? any help is appreciated.... thanks in advance
