jeudi 1 janvier 2015

Simple if else onclick then do?



  1. How do I make it so if yes button clicked change colour?

  2. Is using .onclick the best option for this?

  3. Am I doing it the optimal way?


Thanks.


html:



<body>
<div id="box"></div>
<button id="yes">yes</button>
<button id="no">no</button>
<script src="js/script.js"></script>
</body>


css:



#box {
width: 200px;
height: 200px;
background-color: red;
}


js:



function Choice () {
var box = document.getElementById("box");
var yes = document.getElementById("yes");
var no = document.getElementById("no");

if (yes.clicked == true) {
box.style.backgroundColor = "red";
} else if (no.clicked == true) {
box.style.backgroundColor = "green";
} else {
box.style.backgroundColor = "purple";
};
};

Choice ();




Aucun commentaire:

Enregistrer un commentaire