I have a div with ID "box". its initial background color is set to #ff7700
. I want to create a function "function1()" to do following:
- first click: change the background color to
#ff0077
- Second click: change the background color back to
#ff7700
- third click: change the background color to
#ff0077
- fourth click: change the background color back to
#ff7700
and so on.
Code:
<style>
#box{
background-color: #ff7700;
cursor: pointer;
}
</style>
<div id="box" onClick="function1()" > Inner HTML</div>
I tried to write a function but it is not working.
<script>
function function1(){
var check = document.getElementById("box").style;
check2=check.backgroundColor;
if (check2 != "#ff0077"){
check2="#ff7700";
} else {
check2="#ff0077";
}
}
</script>
please advise any other approach. I want to stick to core JS instead of using Jquery.