I’m creating a quiz and I can’t check with javascript

Asked

Viewed 452 times

0

What I’m trying to do is select the alternative and when checking show with Alert whether the answer is right or wrong. With each correct answer, you add up the correct ones and if you made a mistake, you add up mistakes. But checking the answer 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>
              

1 answer

0

Well from what I understand, you need to issue an alert and add a hit if the answer is right, I made some changes to your html and re-did 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 was selected

if(resposta.checked){}

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

  • Thank you Richard Santos managed to issue Alert but I can’t show the number of hits and the number of errors.

  • Okay, I’m sorry I only got to answer today.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.