How to Clear Input Fields in Javascript
Input fields in a form are commonly used on websites to collect user input. Sometimes, you may need to clear the input fields using Javascript. In this tutorial, we will show you how to clear input fields in Javascript.
HTML Form
Let’s start with a simple HTML form with input fields that we want to clear using Javascript.
Javascript Function
Now, let’s create a Javascript function that will clear the input fields when the button is clicked.
function clearFields() {
document.getElementById(‘firstName’).value = ”;
document.getElementById(‘lastName’).value = ”;
document.getElementById(’email’).value = ”;
}
Testing the Clear Function
Once you have created the clearFields() function, go ahead and test it by entering some text into the input fields and then clicking the “Clear Fields” button. You should see that the input fields are cleared.
Conclusion
Clearing input fields using Javascript is a simple and useful task that can improve the user experience on your website. By following the steps outlined in this tutorial, you can easily clear input fields in your forms using Javascript.