dimanche 2 août 2020

I'm using a function in JS that changes the color to red and then to green onclick

I'm using a function in JS that changes the color to red and then to green onclick. When I click the div, it changes to red, but when I click it again, it stays red, when it's supposed to change back to green. I looked over my code multiple times, but I can't figure out how to fix my problem.

<html>

<head>

<title>Test</title>

</head>

<body>

<div id="c" onclick="func()"></div>

<style>
#c{
background:#00ff00;
width:100px;
height:100px;
}
</style>

<script>
var c = document.getElementById('c');
function func(){
if(c.style.background = "#00ff00"){
c.style.background = "#ff0000";
}
else if(c.style.background = "#ff0000"){
c.style.background = "#00ff00";
}
}
</script>

</body>

Thanks for the help!




Aucun commentaire:

Enregistrer un commentaire