/    Sign up×
Community /Pin to ProfileBookmark

FOR loop question

I am a student and creating a mock questionnaire…whats rong with this? because it doesnt run in my browser?

<!DOCTYPE html>
<html>
<head><title>GradingSheet</title>
</head>
<style>
</style>
<body>
<p>1. What is my name?</p>
<input id=”jong” style type=”radio” value=”a”>a. John</input>
<input id=”jong2″ style type=”radio”>b. Johnx</input>
<input id=”jong3″ style type=”radio”>c. Johnyx</input>
<p>2. What is my last name?</p>
<input id=”jong” style type=”radio”>a. Lenon</input>
<input id=”jong2″ style type=”radio”>b. Lenox</input>
<input id=”jong3″ style type=”radio” value=”v”>c. Lennon</input><br><br>
<button onclick=”myFunction()” type=”submit” value=”Submit”>Submit</button><br><br>
<input id=”result” style=”text” name=”Score”></input>

<script>
function myFunction() {
var a = document.getElementById(“jong”);
var b = document.getElementById(“jong3”);
var score = 0
var c;

for (c=0; c<a.length; c++){
if(a[c].checked) {
if (a[c].value == “a”){score++}
}
}
for (c=0; c<b.length; c++){
if(b[c].checked) {
if (b[c].value == “b”){score++}
}
}
if (score == 2) {message= score
}
document.getElementById(“result”).value = message;
}
</script>

</body>
</html>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERSep 18.2016 — You must use UNIQUE id values

You have at least 2 choices

for each element named 'jong', 'jong2' and 'jong3'

Please put you code between [ code] and [ /code] tags (without the spaces)

to make it easier to read your script and maintain formatting.

<i>
</i>&lt;p&gt;1. What is my name?&lt;/p&gt;
&lt;input [COLOR="#FF0000"]id="jong"[/COLOR] style type="radio" value="a"&gt;a. John&lt;/input&gt;
&lt;input id="jong2" style type="radio"&gt;b. Johnx&lt;/input&gt;
&lt;input id="jong3" style type="radio"&gt;c. Johnyx&lt;/input&gt;
&lt;p&gt;2. What is my last name?&lt;/p&gt;
&lt;input [COLOR="#FF0000"]id="jong"[/COLOR] style type="radio"&gt;a. Lenon&lt;/input&gt;
&lt;input id="jong2" style type="radio"&gt;b. Lenox&lt;/input&gt;
&lt;input id="jong3" style type="radio" value="v"&gt;c. Lennon&lt;/input&gt;


In the following, which of the inputs above is referenced???
<i>
</i>function myFunction() {
var a = document.getElementById([COLOR="#FF0000"]"jong"[/COLOR]);
var b = document.getElementById([COLOR="#FF0000"]"jong3"[/COLOR]);


Note also that 'a' above does not have a length associated with it.

Perhaps you mean the 'options' of the radio selections for the question, but they are not referenced as you have in the subsequent code.

Plus, the 'radio' inputs need a common name associated with the elements for the checked buttons to be dependent

(as opposed to being independent as a checkbox option would be)


And 'style' in the middle of the <input ... statements above will create errors.

Check the error console of the browser for syntax errors as you have created.
Copy linkTweet thisAlerts:
@rootSep 19.2016 — also form inputs use name="" where the name can be grouped under individual or a single name.

[code=html]<!DOCTYPE html>
<html>
<head><title>GradingSheet</title>
<script>
function myFunction( fm ) {
var a = fm.fname,
b = fm.lname,
score = 0,
message = "Try Again";

for (var c=0; c<a.length; c++){
if( a[c].checked ) score += a[c].value-0;
if( b[c].checked ) score += b[c].value-0;
}

if (score == 2) {
message= "Your score = " + score;
}
fm.result.value = message;
}
</script>

</head>
<style>
</style>
<body>
<form name="test" action="javascript:;" onsubmit="myFunction( this );" >
<p>1. What is my name?</p>
<input name="fname" type="radio" value="1">a. John<br>
<input name="fname" type="radio" value="0">b. Johnx<br>
<input name="fname" type="radio" value="0">c. Johnyx<br>
<p>2. What is my last name?</p>
<input name="lname" type="radio" value="0">a. Lenon<br>
<input name="lname" type="radio" value="0">b. Lenox<br>
<input name="lname" type="radio" value="1">c. Lennon<br><br>
<input name="Submit" type="submit" value="Submit"><br><br>
<input name="result" style="text" name="Score"></input>
</form>
</body>
</html>
[/code]


If you use form elements, use a form...
Copy linkTweet thisAlerts:
@GiolancerauthorSep 19.2016 — Thank you guys

also form inputs use name="" where the name can be grouped under individual or a single name.

[code=html]<!DOCTYPE html>
<html>
<head><title>GradingSheet</title>
<script>
function myFunction( fm ) {
var a = fm.fname,
b = fm.lname,
score = 0,
message = "Try Again";

for (var c=0; c<a.length; c++){
if( a[c].checked ) score += a[c].value-0;
if( b[c].checked ) score += b[c].value-0;
}

if (score == 2) {
message= "Your score = " + score;
}
fm.result.value = message;
}
</script>

</head>
<style>
</style>
<body>
<form name="test" action="javascript:;" onsubmit="myFunction( this );" >
<p>1. What is my name?</p>
<input name="fname" type="radio" value="1">a. John<br>
<input name="fname" type="radio" value="0">b. Johnx<br>
<input name="fname" type="radio" value="0">c. Johnyx<br>
<p>2. What is my last name?</p>
<input name="lname" type="radio" value="0">a. Lenon<br>
<input name="lname" type="radio" value="0">b. Lenox<br>
<input name="lname" type="radio" value="1">c. Lennon<br><br>
<input name="Submit" type="submit" value="Submit"><br><br>
<input name="result" style="text" name="Score"></input>
</form>
</body>
</html>
[/code]


If you use form elements, use a form...[/QUOTE]
×

Success!

Help @Giolancer 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 5.7,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...