...

View Full Version : Checking which radio button is checked?



NanakiXIII
12-25-2003, 02:21 PM
Is it possible to check which radiobutton from a group is checked with JS? I can't find anything about this on the net, but I find it a bit hard to believe it's not possible.

A second question, I've seen some scripts where a variable i was used (something like for (i = 0 ; i < 5 ; i++) ) where I couldn't find what i was. Could someone explain what this is?

Mr J
12-25-2003, 08:05 PM
Here's a simple example


<script language="Javascript">
<!--
function resetAll(){ //checks to see if any radio buttons are selected
formfield=document.f1.rad // formname and input names to be checked
for(i=0;i<formfield.length;i++)
if (formfield[i].checked==true){ // if selected
formfield[i].checked=false // deselects
document.getElementById("itext").innerHTML="Which one will you choose?" // the following 3 lines resets text back to original
document.getElementById("itext").style.color=""
document.getElementById("itext").style.fontWeight=""
}
}

function whichradio(){ // check for a selected radio button
formfield=document.f1.rad // formname and input names to be checked
for(i=0;i<formfield.length;i++)
if (formfield[i].checked==true){ // identify selected radio button
document.getElementById("itext").innerHTML=("You have chosen radio button "+formfield[i].value) // show result
document.getElementById("itext").style.color="black"
document.getElementById("itext").style.fontWeight="bold"
}
}

// -->
</script>


<form name=f1>
<P>
<input type=radio name=rad value=one onclick="whichradio()">one
<input type=radio name=rad value=two onclick="whichradio()">two
<input type=radio name=rad value=three onclick="whichradio()">three


<P><input type=button value=Reset onclick="resetAll()">
</form>
<div id=itext>Which one will you choose?</div>

NanakiXIII
12-25-2003, 11:15 PM
Thanks alot. Now I'm trying to use this for creating a survey or quiz, but my script is not working. Could someone tell me what I've done wrong?



<html>
<head>

<script language="javascript">
function score()
{
var answer1 = document.quiz.q1
for (i = 0 ; 1 < answer1.length ; i++)
if (answer1[i].checked == true)
{
var q1 = answer1[i].value
}
var answer2 = document.quiz.q2
for (i = 0 ; 1 < answer2.length ; i++)
if (answer2[i].checked == true)
{
var q2 = answer2[i].value
}
var total = q1 + q2
alert (total)

}

</script
</head>
<body>

<form name="quiz">
Question one:<BR>
one: <input type="radio" name="q1" value=1><BR>
two: <input type="radio" name="q1" value=2><BR>
three: <input type="radio" name="q1" value=3><BR>
four: <input type="radio" name="q1" value=4><BR>
<BR>
Question two:<BR>
one: <input type="radio" name="q2" value=1><BR>
two: <input type="radio" name="q2" value=2><BR>
three: <input type="radio" name="q2" value=3><BR>
four: <input type="radio" name="q2" value=4><BR>
<input type="button" name="submit" value="submit" onClick="score()">
</form>
</body>
</html>

yao_jw
12-26-2003, 02:03 AM
you wrote loop variable i as 1 by mistake

Mr J
12-26-2003, 11:01 AM
<html>
<head>

<script language="javascript">
chkd1=0
chkd2=0
function score(){
var answer1 = document.quiz.q1
for (i = 0 ; i < answer1.length ; i++)

if (answer1[i].checked == true){
var q1 = answer1[i].value
chkd1=1
}

if(chkd1==0){
alert("Please select an answer from questions 1")
chkd1=0
return
}

var answer2 = document.quiz.q2
for (i = 0 ; i < answer2.length ; i++)

if (answer2[i].checked == true){
var q2 = answer2[i].value
chkd2=1
}

if(chkd2==0){
alert("Please select an answer from questions 2")
chkd2=0
return
}

var total = q1 + q2 // gives a concatenated answer
alert (total)

var total2 = (q1*1) + (q2*1)
alert (total2)
}

</script
</head>
<body>

<form name="quiz">
Question one:<BR>
one: <input type="radio" name="q1" value=1><BR>
two: <input type="radio" name="q1" value=2><BR>
three: <input type="radio" name="q1" value=3><BR>
four: <input type="radio" name="q1" value=4><BR>
<BR>
Question two:<BR>
one: <input type="radio" name="q2" value=1><BR>
two: <input type="radio" name="q2" value=2><BR>
three: <input type="radio" name="q2" value=3><BR>
four: <input type="radio" name="q2" value=4><BR>
<input type="button" name="submit" value="submit" onClick="score()">
</form>
</body>
</html>

NanakiXIII
12-26-2003, 04:03 PM
~Slaps forehead~ Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum