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

306
Views
My preventDefault is not working when submit is pressed

I am currently working to learn different Javascript features. I am working with preventDefault. I was attempting to practice on the website I am making, but it is still refreshing the page. What am I doing wrong?

This is my code:

formSubmit.addEventListener('click', function(event) {
  event.preventDefault();
});
<form role="form">
  <label for="fname" class="labelI">First Name:</label> <br>
  <input type="text" name="fName" id="fname" required> <br>
  <label for="lName" class="labelI"></label>Last Name:</label> <br>
  <input type="text" name="lName" id="lName" required> <br>
  <label for="emailA" class="labelI"></label>Email Address:</label> <br>
  <input type="email" name="Email" id="emailA" required><br>
  <label for="password" class="labelI"></label>Password</label> <br>
  <input type="password" name="password" id="password" minlength="5" maxlength="12"> <br>
  <input type="submit" value="Submit" class="but" id="formSubmit">
  <input type="reset" value="Clear" class="but">
</form>

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

0

  • Your DOM might not be read and ready. Always place non defer <script> tags right before the closing </body> tag.
  • Try not to use IDs as immediate window properties for your ID selectors. Even if this might work for historical reasons, always create a variable reference to your DOM elements using querySelector() or querySelectorAll() Methods
  • Don't use "click" Event when you actually want to listen to a "submit" event on the FORM itself. Such will also allow the browser to ignite the input-type attributes like required pattern etc.

<body>
    <form role="form" id="myForm">
        <label for="fname" class="labelI">First Name:</label> <br>
        <input type="text" name="fName" id="fname" required> <br>
        <label for="lName" class="labelI"></label>Last Name:</label> <br>
        <input type="text" name="lName" id="lName" required> <br>
        <label for="emailA" class="labelI"></label>Email Address:</label> <br>
        <input type="email" name="Email" id="emailA" required><br>
        <label for="password" class="labelI"></label>Password</label> <br>
        <input type="password" name="password" id="password" minlength="5" maxlength="12"> <br>
        <input type="submit" value="Submit" class="but" id="formSubmit">
        <input type="reset" value="Clear" class="but">
    </form>

    <!-- Place all your <script>s right BEFORE the closing BODY tag -->
    <script>
        const EL_myForm = document.querySelector("#myForm");
        EL_myForm.addEventListener("submit", function (event) {
            event.preventDefault(); // prevent default browser SUBMIT action
            console.log("Form not submitted yet. Do your JS magic here!");
        });
    </script>
</body>

about 4 years ago · Juan Pablo Isaza Report

0

formSubmit is not a variable. You must grab a reference to an element.

document.getElementById('formSubmit').addEventListener('click', function (event) {
    event.preventDefault();
});
about 4 years ago · Juan Pablo Isaza Report

0

You need to select button with id formSubmit.

var sel = document.getElementById("formSubmit");
sel.addEventListener('click',function(e){
    e.preventDefault();
});

You cant add event listener directly to id 'formSubmit'

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!