I'd like to change css property depend on if-condition, what mean if the user press right key change background with specific color and if he press again change background color to other color.
$(document).ready(function() {
$(document).keyup(function(e) {
var counter = 0;
switch (e.which) {
case 39: // right
if (counter == 1) {
$(".my-div").css("background-color", "red");
counter = counter + 1;
}
if (counter == 2) {
$(".my-div").css("background-color", "yellow");
counter = counter + 1;
}
if (counter == 3) {
$(".my-div").css("background-color", "green")
counter = counter + 1;
}
break;
default:
return; // exit this handler for other keys
}
e.preventDefault(); // prevent the default action (scroll / move caret)
});
console.log(counter);
})
.my-div {
width: 1170px;
height: 500px;
margin: 50px auto;
background-color: #0026ff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="my-div"></div>
counterto0. Then you check for1,2, or3. So nothing happens.