change color javascript onclick code example

Example 1: javascript change color of button

document.getElementById("button").style.background='#000000';

Example 2: onclick change text color javascript

<script>

    document.getElementById('change').onclick = changeColor;   

    function changeColor() {
        document.body.style.color = "purple";
        return false;
    }   

</script>

Example 3: css onclick change color

<style>
.dabutton:focus {
    background-color:yellow;
}
</style>
<button class="dabutton"></button> // <-- usage

Example 4: change color by click event javascript

var button = document.querySelector("button");

button.addEventListener("click", function() {
    const curColour = document.body.style.backgroundColor;

    document.body.style.backgroundColor = curColour === 'red' ? 'blue' : 'red';
});

Example 5: javascript change color

var span = document.getElementsByTagName("span")[0];
span.style.color = "red";

Tags:

Html Example