Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Checking which radiobutton is checked?

    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?

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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>

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

    Code:
    <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>

  • #4
    New Coder
    Join Date
    Nov 2003
    Location
    china
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you wrote loop variable i as 1 by mistake

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    <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>

  • #6
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ~Slaps forehead~ Thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •