0

I am using javascript and trying to change the colour of a button clicked using event interface but in hex format but the browser is saying it doesn't recognise it, but I don't seem to have an issue with this using written colours ('green' for example). Is it impossible to set colour using hex values, if it isn't what am I doing wrong? Code below:

e.currentTarget.style.backgroundColor = '#F0F0F0'

EDIT: I think I may have found out what the problem is, I am trying to apply another colour to the event after the first colour change. Could this be the problem? See below:

if (e.currentTarget.innerHTML == questions[index].correct) {
                e.currentTarget.style.backgroundColor = "green";
                function revertBtnColour2(){
                    e.currentTarget.style.backgroundColor = '#F0F0F0';
                }
                if(e.currentTarget.style.backgroundColor = "green"){
                    setTimeout(revertBtnColour2, 200)
                }
SFleevo
  • 35
  • 5
  • 3
    `e.currentTarget.style.backgroundColor = '#F0F0F0'` should work, are you certain your not missing the color change as `#F0F0F0` in RGB would be `240,240,240` which is pretty close to `white` and may look like there is no change to the background color. – dale landry Nov 28 '22 at 00:18
  • Works fine ~ https://jsfiddle.net/rj9butgy/. What do you mean by _"the browser is saying it doesn't recognise it"_? – Phil Nov 28 '22 at 00:35
  • The console in the browser is giving me this error: Cannot read properties of null (reading 'style') at revertBtnColour2 – SFleevo Nov 28 '22 at 00:43
  • You could make your life easier by adding specific classes that can be toggled when your answers are correct/false. – dale landry Nov 28 '22 at 00:52
  • 1
    Duplicate of [Why currentTarget value is null](https://stackoverflow.com/q/66085763/283366) – Phil Nov 28 '22 at 00:54

0 Answers0