Home | About | Categories | All Tips & Tutorials

Clear all form fields using Javascript

ID: 202

Category: Vanilla Javascript

Added: 25th of October 2020

Views: 663

What does the script do
The simple code below clears a webform by using the input type button and onclick event that calls the clear_form function and clears the form fields, by getting the form id element.

<form id="form">
First Name: <input type="text" name="first_name" value="">
Surname: <input type="text" name="first_name" value="">
<input type="button" onclick="clear_form()" value="Clear Form"> </form>

<script type="text/javascript">
function clear_form()
{
// Clear the input fields
document.getElementById("form").reset();
}
</script>