I'm creating a quiz and I can not do the verification with javascript

0

What I am trying to do is to select the alternative and to check with the alert if the answer is right or wrong. Each correct answer is adding the correct answers and if you make mistakes, you will add errors.  But when checking the response does not show the alert is sent to the url.

      var acertos=0;
var erros=0;

function verificar(){
  var form =document.getElementByName('form');
  var correctanswer =document.getElementByName("B");
form.addEventListener('onclick', function(event) {
    event.preventDefault();
    if (correctanswer.checked) {
       acertos== acertos++;
        alert("Resposta Correta!"+ acertos);
    }else{
        
       alert("Resposta Errada!" + erros );
    }


});
}
        
<div class="col-md-10">
                    <h2>ALFABETO</h2> 

                         <video controls="true" width="500" height="300" id="01" src="video/video1.mp4"></video>
                            <form name ="form">
                                <p>Selecione a opção que corresponde a letra sinalizada</p>
                                <label>
                                    <input type="radio" name="resposta1" value="A"/> Letra A</label>
                                <label>
                                    <input type="radio" name="resposta1" value="B"/> Letra B</label><br>
                                <label>
                                    <input type="radio" name="resposta1" value="C"/> Letra C</label>
                                <label>
                                    <input type="radio" name="resposta1" value="D"/> Letra D</label><br>
                                <button onclick="verificar()">verificar</button>


                    </form>
              
    
asked by anonymous 12.05.2018 / 04:00

1 answer

0

Well, as I understand it, you need to issue an alert and add a hit if the answer is right, I made some changes to your html and re-edited the script

function verificar(){
      var resposta = document.getElementById("ib");
      var erros = 0;
      var acertos = 0;
      if(resposta.checked){
        acertos++;
        alert("ACERTOU - Numero de acertos: " + acertos);
      }
      else{
        erros++;
        alert("ERRADO - Numero de erros: " + erros);
      }
    }
<!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width">
        <title>repl.it</title>
        <link href="index.css" rel="stylesheet" type="text/css" />
      </head>
      <body>
        <script src="index.js"></script>
        <div class="col-md-10">
          <h2>ALFABETO</h2> 
            <video controls="true" width="500" height="300" id="01" src="video/video1.mp4"></video>
            <p>Selecione a opção que corresponde a letra sinalizada</p>
            <label>
                <input type="radio" name="resposta1" value="A"/> Letra A</label>
            <label>
                <input type="radio" name="resposta1" id="ib" value="B"/> Letra B</label><br>
            <label>
                <input type="radio" name="resposta1" value="C"/> Letra C</label>
            <label>
                <input type="radio" name="resposta1" value="D"/> Letra D</label><br>
            <button onclick="verificar()">verificar</button>
        </div>
      </body>
    </html>

In this condition it checks if the element with id ib has been selected

if(resposta.checked){}

I hope to have helped, in case of doubts you can comment that I will respond.

    
12.05.2018 / 05:45