I have button in html
<input type="button" value="Clear"> <textarea id='output' rows=20 cols=90></textarea>
If I have an external javascript (.js) function, what should I write?
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 input values after form submit in React: Store the values of the input fields in state variables. Set the onSubmit prop on the form element. When the submit button is clicked, set the state variables to empty strings.
Change in your html with adding the function on the button click
<input type="button" value="Clear" onclick="javascript:eraseText();"> <textarea id='output' rows=20 cols=90></textarea>
Try this in your js file:
function eraseText() { document.getElementById("output").value = ""; }
You need to attach a click
event handler and clear the contents of the textarea from that handler.
HTML
<input type="button" value="Clear" id="clear"> <textarea id='output' rows=20 cols=90></textarea>
JS
var input = document.querySelector('#clear'); var textarea = document.querySelector('#output'); input.addEventListener('click', function () { textarea.value = ''; }, false);
and here's the working demo.
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