www.webdeveloper.com
Results 1 to 5 of 5

Thread: Get/Compare radio Value

  1. #1
    Join Date
    Mar 2013
    Posts
    14

    Get/Compare radio Value

    Trying to get the value of the radio box and compare it. I think Im suppose to be checking a element but not sure.

    <script type="text/javascript" >
    var intQuestion = Math.floor(Math.random() * 1);
    var intTracker = 0;
    var intAnswer = 0;

    function GenQuestion()
    {
    switch (intQuestion)
    {
    case 0:
    //Question
    document.write("What is 1 + 1 = ? <br/>");

    //Answer 1
    document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"1\" />");
    document.write("2 <br/>");

    //Answer 2
    document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"2\" />");
    document.write("3 <br/>");

    //Answer 3
    document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"3\" />");
    document.write("4 <br/>");

    //Answer 4
    document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"4\" />");
    document.write("5 <br/>");
    break;

    case 1:
    //Question
    document.write("What is 1 + 1 = ? <br/>");

    //Answer 1
    document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"1\" />");
    document.write("2 <br/>");

    //Answer 2
    document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"2\" />");
    document.write("3 <br/>");

    //Answer 3
    document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"3\" />");
    document.write("4 <br/>");

    //Answer 4
    document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"4\" />");
    document.write("5 <br/>");
    break;
    }
    }

    function GenAnswers()
    {
    switch (intQuestion)
    {
    case 0:
    if(document.group1.valueOf() = 1)
    alert("hit 1");
    else
    alert("hit 2");
    break;

    case 1:
    if (document.group1.valueOf() = 1)
    alert("hit 1");
    else
    alert("hit 2");
    break;
    }
    }

    //Test
    GenQuestion()
    </script>

  2. #2
    Join Date
    Mar 2013
    Posts
    14
    sorry forgot to put code tag in and i cant see the edit post button weird

  3. #3
    Join Date
    Mar 2013
    Posts
    14
    I got this far but now it throws errors:

    Code:
    <script type="text/javascript" >
        var intQuestion = Math.floor(Math.random() * 1);
        
        function GenQuestion() 
        {
            switch (intQuestion)
                {
                    case 0:
                        //Question
                        document.write("What is 1 + 1 = ? <br/>");
    
                        //Answer 1
                        document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"1\"/>");
                        document.write("2 <br/>");
    
                        //Answer 2
                        document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"2\"/>");
                        document.write("3 <br/>");
    
                        //Answer 3
                        document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"3\"/>");
                        document.write("4 <br/>");
    
                        //Answer 4
                        document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"4\"/>");
                        document.write("5 <br/>");
                    break;
    
                case 1:
                    //Question
                    document.write("What is 1 + 1 = ? <br/>");
    
                    //Answer 1
                    document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"1\"/>");
                    document.write("2 <br/>");
    
                    //Answer 2
                    document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"2\"/>");
                    document.write("3 <br/>");
    
                    //Answer 3
                    document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"3\"/>");
                    document.write("4 <br/>");
    
                    //Answer 4
                    document.write("<input type=\"radio\" name=\"group1\" onclick=\"GenAnswers();\" value=\"4\"/>");
                    document.write("5 <br/>");
                    break;
                 }
     }
    
      function GenAnswers(obj) 
       {
           var Answer = obj.value;
    
            switch (intQuestion) 
            {
                case 0:
                    if(Answer = 1)
                    {
                        alert("hit 1");
                    }
                    break;
    
                case 1:
                    if(Answer = 1)
                    {
                        alert("hit 1");
                    }
                    break;
    }
    }
    
    //Test
        GenQuestion()

  4. #4
    Join Date
    Mar 2013
    Posts
    14
    I'm just going to post a link to the current code because the restrictions for code posing on here. Anyway, Ive got a response but he suggested Jquery method and I want to use javascript. Help please

    Link:
    http://forums.asp.net/t/1903346.aspx/1?Causing+a+error+

  5. #5
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    This might give you some ideas:
    Code:
    function readRadio(parm) {
      var sel = document.getElementsByName("S"+(6-parm));
      for (var i=0; i<sel.length; i++) {
        if (sel[i].checked == true) return i;
      }
    }
    There are six sets of radio buttons (Sn) to represent how each guitar string is played in creating a chord in guitar.htm.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles