0

I'm writing a HTML webPage, I use a button and I need to change his background-color on mouseover using a javascript function. Here is the code.

<button onmouseover="funzione(this)">BTN</button>
<script>
    funzione(x){
        x.style.background-color=#ffffff;
    }
</script>
1
  • css property are always camelized in js Commented Jun 16, 2017 at 7:27

5 Answers 5

1

In JavaScript you need get handler to element x e.g.:

var x = document.getElementById("x");

but you should do this with CCS:

button{
    background-color: yellow;
}

button:hover{
    background-color: lime;
}

https://developer.mozilla.org/en-US/docs/Web/CSS/:hover

Sign up to request clarification or add additional context in comments.

Comments

0

This is very basic. You're missing keyword function, and with that style of accessing properties, you need to use camel case. Finally, wrap the color hex within quotes.

function funzione(x){
   x.style.backgroundColor= '#ffffff';
}
<button onmouseover="funzione(this)">BTN</button>

The recommended way is use CSS in production, but if you're just into JavaScript and want to explore, it's fine.

Comments

0

If you want in js:

<button onmouseover="set_color(this)">BTN</button>
<script>
function set_color(x){
	x.style.backgroundColor='red';
}
</script>

Comments

0

function funzione(x){
  x.style.backgroundColor = "#ffffff";
}
<button onmouseover = "funzione(this)">BTN</button>

Comments

0

You can do this simply in CSS, its easy, efficient and less code.

<style type="text/css">
.myHoverButton:hover { background-color: #ffffff; }
</style>

<button id="button1" class="myHoverButton">

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.