Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
My Function will not inject text into my HTML

I am creating a quiz for my web site and I would like for my function to be able to inject text into my HTML. It has worked for every other function but this one does not work. I have tried to change my injection method. I have also tried to use a JQuery method to inject my text but it still wont let me.

function quiz() {
  var f = 0;
  alert(f);
  if (document.getElementById("answer1").checked == true) {
    f++;
  };
  if (document.getElementById("answer2").checked == true) {
    f++;
  };
  if (document.getElementById("answer3").checked == true) {
    f++;
  };
  if (document.getElementById("answer4").checked == true) {
    f++;
  };

  function checkpass() {
    var a = ""
    if (f == 4) {
      a = "Y"
      alert(a)
    } else {
      a = "N"
      alert(a)
    };
  };
  checkpass();
  alert(document.getElementById("score"))
  document.getElementById("score").innerHTML = `Score:${f}/4 ${a}`;
};
<div id="WebBody">
  <P>Answer True or False on qustions</P>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer1" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer2" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer3" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer4" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <button onclick="quiz()">Submit</button>
  <p id="score">Score:Not Submitted</p>
</div>

almost 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Your problem is here:

document.getElementById("score").innerHTML = `Score:${f}/4 ${a}`;

You are trying to access the variable "a" that's inside checkpass function, You need to put the variable outside from the checkpass functions.

This is your code

function checkpass() {
    var a = ""
    if (f == 4) {
      a = "Y"
      alert(a)
    } else {
      a = "N"
      alert(a)
    };
  };

It should be something like this:

var a = "";
function checkpass() {
    if (f == 4) {
      a = "Y"
      alert(a)
    } else {
      a = "N"
      alert(a)
    };
  };

And by the way, variable "f" is declared correctly it's not the problem because the error says "message": "Uncaught ReferenceError: a is not defined" and variable "a" is after variable "f" So it's more like it can't access variable "a" not var "f"

almost 4 years ago · Santiago Trujillo Relatório

0

<!DOctyPE html>
<html lang="en">
<body>


<div id="WebBody">
  <P>Answer True or False on qustions</P>
  <p>Is ___</p>
              <form>
                    <input class="Radio-Button" id="answer1" name="T/F" type="radio">
                    <label>True</label><br>
                    <input class="Radio-Button" name="T/F" type="radio">
                    <label>False</label>
              </form> 
              <hr>
  <p>Is ___</p>
              <form>
                    <input class="Radio-Button" id="answer2" name="T/F" type="radio">
                    <label>True</label><br>
                    <input class="Radio-Button" name="T/F" type="radio">
                    <label>False</label>
              </form> 
              <hr>
  <p>Is ___</p>
              <form>
                    <input class="Radio-Button" id="answer3" name="T/F" type="radio">
                    <label>True</label><br>
                    <input class="Radio-Button" name="T/F" type="radio">
                    <label>False</label>
              </form> 
              <hr>
  <p>Is ___</p>
              <form>
                    <input class="Radio-Button" id="answer4" name="T/F" type="radio">
                    <label>True</label><br>
                    <input class="Radio-Button" name="T/F" type="radio">
                    <label>False</label>
              </form> 
              <hr>
      <button onclick="quiz()">Submit</button>
  <p id="score">Score:Not Submitted</p>
</div>




<script>
function quiz() {
  var f = 0;
  var a = ""
  alert(f);
  if (document.querySelector("#answer1").checked == true){
    f++ ;
  };
  if (document.querySelector("#answer2").checked == true){
    f++ ;
  };
  if (document.querySelector("#answer3").checked == true){
    f++ ;
  };
  if (document.querySelector("#answer4").checked == true){
    f++ ;
  };
  function checkpass(){
    if (f == 4) {
      a = "Y"
      alert(a)
    } else { 
      a = "N"
      alert(a)
    };
  };
  checkpass();
  alert(document.querySelector("#score").textContent);
  document.querySelector("#score").innerHTML = `Score:${f}/4 ${a}`;
};

quiz();
</script>
</body>
</html>
almost 4 years ago · Santiago Trujillo Relatório

0

You just have to move the definition of a to outside checkpass or return it because it's needed in the last line (which is outside checkpass). (I'm showing the latter because another answer already shows the first). You might want to look into the js debugger in devtools, because this kind of slips are not unusual, but easy to catch with debugger.

(Also I changed your "score" element alert because alerts are not like console.log - full, expandable objects will not be displayed, just string.)

function quiz() {
  var f = 0;
  alert(f);
  if (document.getElementById("answer1").checked == true) {
    f++;
  };
  if (document.getElementById("answer2").checked == true) {
    f++;
  };
  if (document.getElementById("answer3").checked == true) {
    f++;
  };
  if (document.getElementById("answer4").checked == true) {
    f++;
  };
  
  function checkpass() {
    var a = "";
    if (f == 4) {
      a = "Y"
      alert(a)
    } else {
      a = "N"
      alert(a)
    };
    return a;
  };
  var a = checkpass();
  alert(document.getElementById("score").outerHTML)
  document.getElementById("score").innerHTML = `Score:${f}/4 ${a}`;
};
<div id="WebBody">
  <P>Answer True or False on qustions</P>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer1" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer2" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer3" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer4" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <button onclick="quiz()">Submit</button>
  <p id="score">Score:Not Submitted</p>
</div>

almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda