/    Sign up×
Community /Pin to ProfileBookmark

Javascript and Radio Buttons

hey all,

I am working on a site for a teacher that has an online exam to it. What I am working on is basically 10 multiple choice questions, each with 4 possibilities. Imagine the html for this as follows (this is arbitrary off the top of my head so I hope that it is right… I don’t work with forms often):

<form name=”test”>
<b> 1) What is 2 plus 2? </b> <br />
<input type=”radio” name=”q1″ value=”1″> one. <br />
<input type=”radio” name=”q1″ value=”2″> two. <br />
<input type=”radio” name=”q1″ value=”3″> three. <br />
<input type=”radio” name=”q1″ value=”4″> four. <br />

THE OTHER NINE QUESTIONS GO HERE!
</form>

ok, so, I know that radio buttons are referred to as an array, such as:

var answer = document.test.q1[1].value;

My question is this: Is there a way to just get the selected value of the array of buttons? By that what I mean is can I just refer to the radio button by name, and obtain the value of the button that is selected. i.e.

var answer = document.test.q1.value?

Now, I know that the above is incorrect, but I am hoping I can just do something like that instead of having to look at EACH button in the “q1” array to find the selected one and its value.

I really hope that makes sense as I am typing in a rush right now. I am asking this so I can write the function that totals the students score, and also tells them which one(s) that they got wrong.

Thanks in advance for any help!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@MikeOSDec 04.2002 — I suppose you could try to capture the answer using an onclick event handler as shown below. This way the entire radio button array isn't checked for an answer. I would be interested to know why you don't want to take advantage of the radio button array!!!


<html>

<head>

<script language="javascript">

var givenanswer;

function panswer(usersanswer)

{

givenanswer = usersanswer

alert(givenanswer)

}

</script>

</head>

<body>

<form name="test">

<b> 1) What is 2 plus 2? </b> <br />

<input type="radio" name="q1" value="1" onclick="panswer(this.value)"> one. <br />

<input type="radio" name="q1" value="2" onclick="panswer(this.value)"> two. <br />

<input type="radio" name="q1" value="3" onclick="panswer(this.value)"> three. <br />

<input type="radio" name="q1" value="4" onclick="panswer(this.value)"> four. <br />

</form>

</body>

</html>
Copy linkTweet thisAlerts:
@James_L_authorDec 04.2002 — Mike,

thanks for your reply. It isn't specifically that I DON'T want to take advantage of the radio array, it is more that I can't seem to wrap my head around the way to structure my script to mark the quiz.

Let me explain... My game plan right now is to load an array with the correct answers to the 10 multiple choice questions. Then, I want a loop that is activated when the "Mark Quiz" button is pushed. This loop would run 10 times, determining which radio button, and hence which value, the student selected for each question. These would be loaded into another array, then the two arrays would be compared to determine the score on the quiz. The student would also be shown the correct answer along with the answer they choose for any questions they got wrong.

What I can't figure out for some strange reason is how to write this loop. It is probably staring me right in the face. I am sure it will come to me but ya know how sometimes you just can't grasp the obvious... well, that's where I am!

If you have anymore help it would be appreciated!

Thanks again.
Copy linkTweet thisAlerts:
@lariaDec 05.2002 — I have a javascript quiz that will mark you as you go through the quiz with either a checkmark or an x. It won't let you change your answer (this is a good thing) and tallies up your score at the end.

The problem I'm having is, when you reset the page, the checked radio buttons from the previous person's answers are still there.

You're welcome to this script if you want it, let me know. If you know how to reload the page and have a blank quiz ready for the next person, I'd love to know how to do that!

Thanks and if I'm off topic, my apologies ?

laria
×

Success!

Help @James_L_ 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.8,
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,
)...