Home > Software design >  I created a DOM event that isn't giving me the result I wanted
I created a DOM event that isn't giving me the result I wanted

Time:02-22

I created a function to change the color value of my elements. I used the same function on both elements with different events but my second element isn't changing only the first element.

function colorChange(event){
  let randomColor = 'rgb('   colorValue()   ','   colorValue()   ','   colorValue()   ')';
  button.style.backgroundColor = randomColor
}

button.addEventListener('click', colorChange);
mysteryButton.onwheel = colorChange;

CodePudding user response:

Maybe you should try instead

function colorChange(event){
  let randomColor = 'rgb('   colorValue()   ','   colorValue()   ','   colorValue()   ')';
  event.target.style.backgroundColor = randomColor
}

button.addEventListener('click', colorChange);
mysteryButton.onwheel = colorChange;
  • Related