0

Element I want to change

<button id="thumb" onfocus="focus()" style="height: 12px; width: 12px; background-color: white; border-radius: 50%; margin: 0 auto; position: relative; top: -3px; left: 50%; box-shadow: 0px 0px 3px #888888;">
</button>

Function called:

function focus() {
    document.getElementById('thumb').style.background-color="blue";
}

The error:

Uncaught ReferenceError: Invalid left-hand side in assignment

I don't understand why this is an error. I think it might be because of the - symbol. It works with other styling e.g. document.getElementById('thumb').style.width="10px" works.

3 Answers 3

3

With dash-ed styles you need to use camelCase. So with

-lowerCase = Uppercase

For Example

background-color : backgroundColor;
font-size : fontSize


 function focus() {
     document.getElementById('thumb').style.backgroundColor="blue";
 }
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

document.getElementById('thumb').style.backgroundColor="blue";

Comments

1

An alternative way instead of using JavaScript is to use CSS to change the colour of the button on focus.

I have provided an example below I hope it helps.

CSS

.item-input:focus {
   background-color: blue;
}

CSS CLASS: Ensure to include class="item-input"

<button id="thumb" class="item-input" onfocus="focus()" style="height: 12px; width: 12px; background-color: white; border-radius: 50%; margin: 0 auto; position: relative; top: -3px; left: 50%; box-shadow: 0px 0px 3px #888888;">
</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.