Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear text area with a button in html using javascript?

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?

like image 405
noobprogrammer Avatar asked Apr 12 '13 10:04

noobprogrammer


People also ask

How do you clear a field in Javascript?

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.

How do you clear textarea After submit react?

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.


2 Answers

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 = ""; } 
like image 184
Alessandro Minoccheri Avatar answered Oct 05 '22 16:10

Alessandro Minoccheri


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.

like image 25
Rishabh Avatar answered Oct 05 '22 15:10

Rishabh