Hello…again ?
I wrote this script:
[code=html]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Radiobuttons: oef</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<script type=”text/javascript”>
/* <![CDATA[ */
function controleer(){
for(i=0;i<document.formulier.option.length;i++){
if(document.formulier.option[i].checked){
var waarde=document.formulier.option[i].value;
if(waarde=”Brussel”){
alert(“Proficiat! Brussel is het correcte antwoord”);
}
else{
alert(“Spijtig. “+waarde+” is niet het correcte antwoord”);
}
}
}
}
/* ]]> */
</script>
</head>
<body>
<h4>Wat is de hoofdstad van België?</h4>
<form name=”formulier” action=””>
<input type=”radio” name=”option” value=”Antwerpen” />Antwerpen<br />
<input type=”radio” name=”option” value=”Brussel” />Brussel<br />
<input type=”radio” name=”option” value=”Gent” />Gent<br />
<input type=”radio” name=”option” value=”Luik” />Luik<br />
<input type=”button” value=”Controleer!” onclick=”controleer();” />
</form>
</body>
</html>
Now the result it produces isn’t correct since I assume JavaScript fails to execute this line properly
[code=html]if(waarde=”Brussel”)
I get the same result no matter what radiobutton is selected:
[code=html](“Proficiat! Brussel is het correcte antwoord”)
This works perfectly:
[code=html]
function controleer(){
for(i=0;i<document.formulier.option.length;i++){
if(document.formulier.option[i].checked){
var waarde=document.formulier.option[i].value;
}
}
if(document.formulier.option[1].checked){
alert(“Proficiat! Brussel is het correcte antwoord”);
}
else{
alert(“Spijtig. “+waarde+” is niet het correcte antwoord”);
}
}
This code is actually the same as the one written at the top of this post except for the explicit stating
[code=html]if(document.formulier.option[1].checked)
vs
[code=html]if(waarde=”Brussel”)
why isn’t the latter possible?