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

88
Visualizações
Collect answer and test it

I am trying to do a scavenger hunt with my students. Basically they do something, input an answer, if it is correct then the page should give them the URL to jump to the next clue. If it is wrong, they get a "Sorry, wrong" message and can keep inputting until they get it. I have read several sites and tried different things. I had the input worked out, but could not figure out how to display a hotlink for them to click on in JS. I have a free site setup and want to insert this Javascript but of course it is not working!

function myFunction() {
  var letter = document.getElementById("personsInput").value;
  var text;
  let linkName = "Next challenge.";
  // If the word is correct “chocolate"
  if (letter === "chocolate") {
    text = "<a ref ='https:// this is where the URL goes'> +linkName + " < /a>";
    // If the word is anything else
  } else {
    text = "Sorry, wrong answer";
  }
  document.getElementById("demo").innerHTML = text;
}
<input id="personsInput" type="text">
<button onclick="myFunction()">What is your answer?</button>
<p id="demo"></p>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

  1. You are missing the closing quotation around "<a ...> + linkname
  2. Your anchor tag needs href, not ref. I fixed these two issues and it works as intended.

<input id="personsInput" type="text">
<button onclick="myFunction()">What is your answer?</button>
<p id="demo"></p>
<script>
    function myFunction() {
        var letter = document.getElementById("personsInput").value;
        var text;
        let linkName = "Next challenge.";
        // If the word is correct “chocolate"
        if (letter === "chocolate") {
            text = "<a href ='https:// this is where the URL goes'>" +linkName + "</a>";
        // If the word is anything else
        } else {
            text = "Sorry, wrong answer";
        }
        document.getElementById("demo").innerHTML = text;
     }
 </script>

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is another way to do it Using document.createElement

<script>
function myFunction() {
  const answer = document.getElementById("personsInput").value;
  const resultArea = document.getElementById("demo")
  const linkName = "Next challenge.";
  const wrongAnswer = "Sorry, wrong answer.";
  resultArea.innerHTML = '';

  if (answer === "chocolate") {
    const link = document.createElement('a');
    link.href = 'http://google.com';
    link.text = linkName;
    link.target = '_blank';
    resultArea.appendChild(link);
   } else {
    text = "Sorry, wrong answer";
    resultArea.innerHTML = wrongAnswer;
   }
  }
</script>
<input id="personsInput" type="text">
<button onclick="myFunction()">What is your answer?</button>
<p id="demo">
</p>

As noted in the other answers maybe this is not the best way to exam your students, as they can view the source and get the correct answers from there.

about 4 years ago · Juan Pablo Isaza Relatório

0

You Got it! You are just missing a closing " after where the URL goes.

A warning your students will be able to inspect the site source and see the correct answers.

text = "<a ref ='https:// this is where the URL goes'>" + linkName + "
 </a>";

function myFunction() {
  var letter = document.getElementById("personsInput").value;
  var text;
  let linkName = "Next challenge.";
  // If the word is correct “chocolate"
  if (letter === "chocolate") {
    text = "<a ref ='https:// this is where the URL goes'>" + linkName + " </a>";
    // If the word is anything else
  } else {
    text = "Sorry, wrong answer";
  }
  document.getElementById("demo").innerHTML = text;
}
<input id="personsInput" type="text">
<button onclick="myFunction()">What is your answer?</button>
<p id="demo"></p>

about 4 years ago · Juan Pablo Isaza 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