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

185
Views
Why isn't my event listener working despite no apparent errors?

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>SwinTech</title>
</head>

<body>
    <form action="apply.html" method="post">
        <div class="consultantinformation">
            <h2> Job reference number: </h2>
            <br>
            <h4 id="jobreference2"> 6LZ9W </h4>
        </div>
        <button id="submitbutton">Submit</button>
    </form>
    <script type="text/javascript">
      document.getElementById("submit3").addEventListener("click", myfunction);
      function myFunction(){
      localStorage.setItem("jobreference3", "KJ3N7");
    }

    </script>
</body>

</html>

<body>
        <form action="https://mercury.swin.edu.au/it000000/formtest.php" method="post" id="regform">
          <p>Job Reference Number: <span id="job1"></span> <span id="job2"></span> <span id="job3"></span>
          </p>
          
          <input type="submit" value="Apply">
          </form>
        <script type="text/javascript">
          document.getElementById("job1").innerHTML = localStorage.getItem("jobreference1");
          document.getElementById("job2").innerHTML = localStorage.getItem("jobreference2");
          document.getElementById("job3").innerHTML = localStorage.getItem("jobreference3");
        </script>
      </body>

I've set up an event listener which makes it so that the value is only stored if the submit button is pressed. For some reason it doesn't work though. I set it up to be retrieved on another page but even if I simply hyperlink myself to that page, without submitting this form, the value is displayed on the other page. Why isn't the even listener working?

edit: I edited in the HTML code that retrieves the values I set. The thing is that it retrieves it and displays it even if I don't submit the form on the other HTML page.

Note: the reason there is "job1", "job2" and "job3" is because I only sent a small portion of the first code. It's meant to only show 1 depending on which form you submitted on the previous page.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to define a function named setItem. That function is the event handler and will react when the event happens

    <script>
    function setItem () {
        localStorage.setItem("jobreference1", "1FN43");
}    document.getElementById("submitbutton").addEventListener("click", setItem);
    </script>
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!