Reset textarea height in javascript

时间秒杀一切 提交于 2019-12-01 07:20:23

问题


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>

回答1:


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>



回答2:


Try resetting the style:

document.getElementById("textarea").style.height = "20px";



回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!