To clear the entire input field without having to delete the whole thing manually Or what if there is already a pre-suggested input present in the input field, that the user doesn’t want. There could be many scenarios. Therefore, in this article, we are going to learn about how to clear the input field.
There are two ways to do this and both are super easy and short. Let us check out both of them.
Method 1: Clearing Input on Focus.
Syntax:
<input onfocus=this.value=''>
Approach:
- Create an input field.
- Set the onfocus attribute to NULL using this.value
Example:
<!DOCTYPE html> <html> <body> <body style="text-align:center"> <h2 style="color:green">GeeksForGeeks</h2> <h2 style="color:purple">Clearing Input Field</h2> <p>The below input field will be cleared, as soon as it gets the focus</p> <input type="text" onfocus="this.value=''" value="Click here to clear"> </body> </html> |
Output:
Before:

After:

Method 2: Clearing Input with the help of button.
Syntax:
<button onclick="document.getElementById('InputID').value = ''>
Approach:
- Create a button.
- Get the id of input field.
- Set the value NULL of input field using document.getElementById(‘myInput’).value = ”
<!DOCTYPE html> <html> <body style="text-align:center"> <h2 style="color:green"> GeeksForGeeks </h2> <h2 style="color:purple"> Clearing Input Field </h2> <p>The below input field will be cleared, as soon as the button gets clicked </p> <button onclick= "document.getElementById( 'myInput').value = ''"> Click here to clear </button> <input type="text" value="GeeksForGeeks" id="myInput"> </body> </html> |
Output:
Before:

After:

Recommended Posts:
- How to put a responsive clear button inside HTML input text field ?
- How to validate if input in input field has base 32 encoded string using express-validator ?
- How to validate if input in input field has base64 encoded string using express-validator ?
- How to validate if input in input field has ASCII characters using express-validator ?
- How to validate if input in input field has BIC or Swift code only using express-validator ?
- How to validate if input in input field has alphanumeric characters only using express-validator ?
- How to validate if input in input field has boolean value using express-validator ?
- How to validate if input in input field has alphabets only using express-validator ?
- How to validate if input in input field exactly equals to some other value using express-validator ?
- Semantic-UI | Input Field
- Set the value of an input field in JavaScript
- How to get the value of text input field using JavaScript?
- JQuery | Set the value of an input text field
- How to validate input field while focusout ?
- How to disable browser Autocomplete on web form field/input tag?
- How to force Input field to enter numbers only using JavaScript ?
- How to place Font Awesome icon to input field ?
- How to detect value change on hidden input field in JQuery ?
- How to Adjust the Width of Input Field Automatically using JavaScript ?
- JQuery | Set focus on a form input text field on page load
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh


