/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Simple JavaScript fails 5

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>
[/code]

Now the result it produces isn’t correct since I assume JavaScript fails to execute this line properly

[code=html]if(waarde=”Brussel”)[/code]

I get the same result no matter what radiobutton is selected:

[code=html](“Proficiat! Brussel is het correcte antwoord”)[/code]

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”);
}
}
[/code]

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)[/code]

vs

[code=html]if(waarde=”Brussel”)[/code]

why isn’t the latter possible?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangDec 28.2009 — Compare to not equal toif(waarde=="Brussel")
Copy linkTweet thisAlerts:
@ajajajakauthorDec 28.2009 — Aha ofcourse...shouldn't make a habit out of these ?

thx again Fang
×

Success!

Help @ajajajak spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...