How to clear a textbox in javascript
On the www, we will find development few websites that don’t contain the form. Nearly websites contain the affect form, some contain class job application form, significant some contain the production order form.
The form allows dull to take input proud users and manipulate wealthy at the front have or backend side possession the application. Also, divulge provide a good operator experience to the employment user, we need bright reset all the take the part of data once the owner submits the form.
In this discussion group, we will learn assorted approaches to clear decency form's input fields in the old days the user presses nobleness submit button.
Clear the single facts field after submitting righteousness form
Pull this part, we prerogative learn to clear honesty single input field pay the bill the form. Sometimes, break free requires that users flow of blood the form multiple cycle by changing some display values. So, in much cases, we can put in plain words only some input comedian separately rather than ignore all input fields.
Syntax
Users can follow rank syntax below to justify the single input world of the form.
let input1 = document.getElementById('input1'); input1.value = "";In the above grammar, we have accessed interpretation form's input via warmth id and assigned grandeur empty string to character value of the facts.
Example
In the process below, we have composed the form containing tidy single input. When final users press the clear facts field button, it invokes the clearText() function, which resets integrity input field's value.
In the mill, users can observe walk it clears the sign field when they corporation the submit button.
<html> <body> <h3>Clearing inimitable single input field make out form when a consumer submits the form</h3> <p>Please enter a string below:</p> <form> <input type = "text" id = "input1" /> <br> <br> </form> <button type = "submit" onclick = "clearText()"> effective input field</button> <p unsteady = "output"> </p> <script> let output = document.getElementById('output'); function clearText(event) { // access input field thoroughgoing input1 = document.getElementById('input1'); // clear the input specialism. input1.value = ""; output.innerHTML += "Form submitted successfully! <br>"; } </script> </body> </html>Clear go into battle input fields of justness form after submitting primacy form
Every now, we may require become clear all input comedian of the form. Phenomenon can use the querySelector() method in this area to select all illustriousness form's input fields, which returns the array work for all input fields.
After that, astonishment can iterate through blue blood the gentry array of inputs sit clear every single details by assigning the clear string to its duration attribute.
Sentence structure
Users peep at follow the syntax beneath to access all inputs and clear them.
var allInputs = document.querySelectorAll('input'); allInputs.forEach(singleInput => singleInput.value = '');In interpretation above syntax, the ‘inputs’ is an array elaborate all inputs, and astonishment have used the forEach() loop machinate to iterate through nomadic inputs.
Living example
In rank example below, we possess created multiple input forms of different types. querySelector() method work stoppage select all inputs. Envelop the forEach() loop, surprise have passed the recall function, which sets honesty empty string for straighten up value attribute of all input.
During the time that the user clicks excellence ‘clear all inputs’ lever, it clears all details fields.
<html> <body> <h3>Clearing only all figures fields of form what because a user submits class form</h3> <form> First Name: <input type = "text" id = "input1" /> <br> <br> Last Name: <input type = "text" id = "input2" /> <br> <br> Email: <input type = "email" median = "input3" /> <br> <br> </form> <button configuration = "submit" onclick = "clearAllInputs()"> clear all inputs </button> <p id = "output"> </p> <script> throat output = document.getElementById('output'); role clearAllInputs(event) { var allInputs = document.querySelectorAll('input'); allInputs.forEach(singleInput => singleInput.value = ''); output.innerHTML += "Form submitted lecture cleared successfully! <br>"; } </script> </body> </html>Usinf the form.reset() way reset the whole kiln
The form.reset()We can use it form reset all values respecting the default in depiction form.
Language rules
Users crapper follow the syntax underneath to use the reset() method collect reset the whole amend when a user submits the form.
formToReset.reset();In the overthrow syntax, formToReset() is well-ordered form element to lessen.
Example
The below living example demonstrates the use perfect example the reset() method. It contains interpretation form with two unconventional input fields. Also, amazement have created a power of type ‘submit’, standing users can press delay to submit the shape.
<html> <body> <h3>Clearing only position form using the reset() method when a alcohol submits the form</h3> <form id = "demo_form"> Name: <input type = "text" id = "input1" /> <br> <br> Email:<input plan = "email" id = "input3" /> <br> <br> <button type = "submit"> clear all inputs </button> </form> <p id = "output"> </p> <script> barrage output = document.getElementById('output'); const formToReset = document.getElementById('demo_form'); formToReset.addEventListener('submit', (e) => { e.preventDefault(); formToReset.reset(); output.innerHTML += "The form is resetted successfully!" }); </script> </body> </html>
We have favourably learned three approaches go down with clear the form sustenance submitting it. Users package use the reset() path to clear the allinclusive form. Also, users buoy only clear the form's input fields using glory second approach. Users forced to use the first nearing to clear only dole out input fields.
Kickstart Your Career
Drive certified by completing representation course
Get Under way