Skip to content Skip to sidebar Skip to footer

How To Clear Text Area With A Button In Html Using Javascript?

I have button in html If I have an external javascript (.js) function, wha

Solution 1:

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 = "";
}

Solution 2:

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.


Solution 3:

<input type="button" value="Clear" onclick="javascript: functionName();" >

you just need to set the onclick event, call your desired function on this onclick event.

function functionName()
{
    $("#output").val("");
}

Above function will set the value of text area to empty string.


Solution 4:

Your Html

<input type="button" value="Clear" onclick="clearContent()"> 
<textarea id='output' rows=20 cols=90></textarea>

Your Javascript

function clearContent()
{
    document.getElementById("output").value='';
}

Solution 5:

You can use this

//use this
function clearTextarea(){
  $('#output').val("");
}
// Or use this it gives the same result
$(document).ready(function () {
$(`input[name="clear-btn"][type="button"]`).on('click',function(){
  $('#output').val("");
})
})
<input type="button" name="clear-btn" value="Clear"  onclick="clearTextarea()"> 
<textarea id='output' rows=20 cols=90></textarea>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

Post a Comment for "How To Clear Text Area With A Button In Html Using Javascript?"