I have two text labels:
<div>
<input type="text" id="textfield1" size="5"/>
</div>
<div>
<input type="text" id="textfield2" size="5"/>
</div>
I would like to have a button called "Clear" that would erase both these fields.
To my knowledge, I know that to implement a button I should use a code like this:
<div>
<input type="button" value="Clear Fields" onclick=SOMETHING />
</div>
Any clues on how to implement SOMETHING
?
To clear an input field after submitting:Add a click event listener to a button. When the button is clicked, set the input field's value to an empty string. Setting the field's value to an empty string resets the input.
To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.
A simple JavaScript function will do the job.
function ClearFields() {
document.getElementById("textfield1").value = "";
document.getElementById("textfield2").value = "";
}
And just have your button call it:
<button type="button" onclick="ClearFields();">Clear</button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With