JavaScript stop form submit

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Forms are a very important part of building apps and websites. They are the key to user input.
Let's look at some examples of forms we can think of:
As you can see, forms are a big pillar in development. And generally, a form will perform a specific type of request to a defined action.
<form action="post.php" method="POST"></form>
Take the above example. It will perform a POST request to a file called post.php.
However, in modern development, we often want to keep the user on the page and do these transactions in the background by using JavaScript.
Think about a search bar, for instance, how annoying it would be if it had to refresh the page every time you change the search query.
That's where handling forms with JavaScript comes in super handy. Let's see how this works.
To catch the form submit in JavaScript, we have to have the form available in our code.
The easiest way would be to add a selector. This could be a querySelector or fetch the form by its unique ID.
<form id="form-id">
<input type="text" name="name" id="name-field" />
<input type="submit" value="submit" />
</form>
const form = document.querySelector('form');
const formId = document.getElementById('form-id');
Both these methods will have the same reference to the form.
However, I would urge you to use the latter one since it's more unique especially if you can have more than one form on a page.
Let's take the ID method and handle the form submit in JavaScript to stop the submit. For this to work, we need to attach an event listener on the form's submit action.
const form = document.getElementById('form-id');
form.addEventListener('submit', (event) => {
alert('submitting');
});
This will perform the following actions:
That's not exactly what we want, as we want the HTML submit not to happen at all.
To intercept that behavior, we have to use the event.preventDefault code.
form.addEventListener('submit', (event) => {
event.preventDefault();
alert('submitting');
});
Now we only get the alert, and our form is stopped!
You can check out the JavaScript form submit in this CodePen.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter