Javascript DOM Form Radio Button Checked

Description

Javascript DOM Form Radio Button Checked

View in separate window

<html>
   <head>
      <script type = "text/JavaScript">

         function checkAnswers()/*from  w w w.  jav a  2  s.  c o  m*/
         {
            let myQuiz = document.getElementById( "myQuiz" );            
           
            // determine whether the answer is correct
            if ( myQuiz.elements[ 1 ].checked )
               console.log( "Congratulations, your answer is correct" );
            else // if the answer is incorrect
               console.log( "Your answer is incorrect. Please try again" );
         }
      </script>
   </head>
   <body>
      <form id = "myQuiz" onsubmit = "checkAnswers()" action = "">
         <p>Select the name of the tip that goes with the 
            image shown:<br />
            <img src="EPT.gif" alt="mystery tip"/>
            <br />

            <input type = "radio" name = "radiobutton" value = "CPE" />
            <label>Common Programming Error</label>

            <input type = "radio" name = "radiobutton" value = "EPT" />
            <label>Error-Prevention Tip</label>

            <input type = "radio" name = "radiobutton" value = "PERF" />
            <label>Performance Tip</label>

            <input type = "radio" name = "radiobutton" value = "PORT" />
            <label>Portability Tip</label><br />

            <input type = "submit" name = "submit" value = "Submit" />
            <input type = "reset" name = "reset" value = "Reset" />
         </p>
      </form>
   </body>
</html>



PreviousNext

Related