I have a textarea (auto resizing) and I want to clear all of its contents including its height.
So far I've tried:
document.getElementById('textarea').value = '';
And :
document.getElementById('textarea').attribute('rows','1');
But both doesn't work.
EDIT :
I use autosize.js with this :
<textarea id="post" rows="1" title="Write something..." name="posttxt" placeholder="Write something..." role="textbox" autocomplete="off"></textarea>
You could use setAttribute to reset style attribute added automatically during the resize :
document.getElementById('reset').onclick = function(){
var textarea = document.getElementById('target');
textarea.setAttribute('style','');
textarea.value = "";
}
<textarea id="target" rows="1" cols="10"></textarea>
<br>
<button id="reset">Reset</button>
Try resetting the style:
document.getElementById("textarea").style.height = "20px";
Just a JQuery version, that I'm using when I close a modal on my project
$("#jobURL").attr("style", "").val("")
来源:https://stackoverflow.com/questions/36087612/reset-textarea-height-in-javascript