Last updated: Mar 5, 2024
Reading timeยท3 min
To clear an input field after submitting:
click
event listener to a button.Here is the HTML for this example.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <input type="text" id="first_name" name="first_name" /> <button id="btn" type="submit">Submit</button> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const btn = document.getElementById('btn'); btn.addEventListener('click', function handleClick(event) { // ๐๏ธ if you are submitting a form (prevents page reload) event.preventDefault(); const firstNameInput = document.getElementById('first_name'); // Send value to server console.log(firstNameInput.value); // ๐๏ธ clear input field firstNameInput.value = ''; });
We added a click
event listener to the button.
handleClick
function is invoked, where we set the value of the input to an empty string.I've also written a tutorial on how to clear the value of a textarea.
To clear the values for multiple inputs after submitting:
querySelectorAll()
method to select the collection.forEach()
method to iterate over the results.value
of each input
field to an empty string to reset it.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <input type="text" id="first_name" name="first_name" /> <input type="text" id="last_name" name="last_name" /> <button id="btn" type="submit">Submit</button> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const btn = document.getElementById('btn'); btn.addEventListener('click', function handleClick(event) { // ๐๏ธ if you are submitting a form event.preventDefault(); const inputs = document.querySelectorAll('#first_name, #last_name'); inputs.forEach(input => { input.value = ''; }); });
We used the document.querySelectorAll
method to select a NodeList
containing the elements with IDs set to
first_name
and last_name
.
The method takes a string that contains one or more valid CSS selectors.
The function we passed to the
NodeList.forEach
method gets invoked with each input in the NodeList
.
In the function, we set the value of each input to an empty string.
I've also written a guide on how to use multiple conditions with querySelectorAll.
To clear all form fields after submitting:
submit
event listener on the form
element.reset()
method on the form.reset
method restores the values of the input fields to their default
state.Here is the HTML for this example:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <form action="" id="my_form"> <input type="text" id="first_name" name="first_name" /> <input type="text" id="last_name" name="last_name" /> <button id="btn" type="submit">Submit</button> </form> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const form = document.getElementById('my_form'); form.addEventListener('submit', function handleSubmit(event) { event.preventDefault(); // ๐๏ธ Send data to the server here // ๐๏ธ Reset the form here form.reset(); });
We added a submit event listener to the form element.
The event fires when a form
is submitted.
We used the event.preventDefault()
method to prevent the page from reloading.
The reset() method restores a form's elements to their default values.
If you need to show/hide a form on a button click, check out the following article.
You can learn more about the related topics by checking out the following tutorials: