как очистить input js

Как видно из кода, принцип довольно прост и заключается в присвоении пустой строки свойству value этого элемента.
Например, мы хотим очистить поле ввода для поиска музыки. Тогда наш код будет выглядеть так:
При нажатии на кнопку "Очистить" значение поля ввода будет заменено на пустую строку, тем самым произойдет очистка.
HTML how to clear input using javascript?
I have this INPUT, it will clear everytime we click inside of it.
The problem: I want to clear only if value = exemplo@exemplo.com
Can someone help me to do this? I don’t know how to compare, I already tried but no success.
9 Answers 9
Is this really what your looking for?
![]()
For me this is the best way:
![]()
![]()
you can use attribute placeholder
or try this for older browsers
![]()
You could use a placeholder because it does it for you, but for old browsers that don’t support placeholder, try this:
CODE EXPLAINED: When the text box has focus, clear the value. When text box is not focused AND when the box is blank, replace the value.
I hope that works, I have been having the same issue, but then I tried this and it worked for me.
Selecting , clearing and focusing on input in Javascript.
![]()
Learn how to select text, focus and clear the text in input box in Javascript.
input.value = "" → this will make the input value to empty string.
input.focus() → this method will set focus on the input box.
input.select() → this will select the text in the input box.
If you find this helpful surprise me here.
Share if you feel happy .
Follow Javascript Jeep if you feel worthy.
JavaScript Clear Input

In JavaScript, we usually consider a specific property to deal with the input value. The value property along with the getElementById() method grabs an input field’s detail.
Later, we can perform some conditions based on the entry. Again, we can also depend on some HTML attributes to connect the input elements and JavaScript functions.
Please enable JavaScript
Here, we will present some examples that can be implemented in multiple ways.
Use the onfocus Attribute to Vanish Input Field in JavaScript
We will initially declare an input field with a defined value for this example. Also, the input element will jump to perform the function clearF(this) as soon the cursor will point or focus in the input box.
If the function’s condition is served, it will remove the value, and thus the input gets cleared.
Its content vanishes as the cursor is placed and clicked on the input field. The this refers to its value for the specific element.
Use Conditional Statement to Clear Form Input Value in JavaScript
According to a form submission, it is necessary to have a corresponding button element to store data. We will take a form element, and the button associated with it will carry an onclick attribute to fire a function.
The function will check if the input field is empty or not. If it finds any content, it will simply reinitialize it empty.
Here, we have an instance of the grab form that will take the id . When the button is clicked, the function will check if the input is empty, and it will perform just as the conditional statement describes.
Use the reset() Method to Erase Form Input Fields in JavaScript
We will also examine a form to clear its input by the reset() method. All it will need is to create an instance of the form, and on the button click, it will reset the whole form vanishing the inserted contents.
Era is an observer who loves cracking the ambiguos barriers. An AI enthusiast to help others with the drive and develop a stronger community.