dimanche 22 février 2015

How do I check if a checkbox is true in HTML and JavaScript?

I'm making a form to check what you can eat if you are allergic. This is my basic form, but I need to check if a checkbox is checked. I tried this, but it doesn't work. The variebles and the text is in Dutch, but you don't have to pay attention to that. Please help me to check if a checkbox is checked. Thank you!



<!doctype html>
<html>
<head>
<title>Selecteer allergieën</title>
<h1>Selecteer je allergieën hieronder</h1>
</head>
<body>
<form>
<label for="pinda">
<input type="checkbox" id="pinda" value="Pinda's">Pinda's
</label><br>
<input type="button" value="Gaan ->" onClick="myFunction()">
</form>
<script>
function myFunction(){

var pinda = document.getElementById("pinda").checked;

if(pinda = checked){
alert("Je bent allergisch voor pinda's");

}
}



</body>
</html>


One var is left, so you can review the code easily.





Aucun commentaire:

Enregistrer un commentaire