Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

87
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!