admin管理员组文章数量:1426652
I have a textarea with the default number of columns, like 10 or 15 or something. I want the text area to shrinkwrap the text inside on blur. Does anyone know how to do this?
So far I have
$('textarea').live('blur', function() {
var textAreaWidth = $(this).val().length;
$(this).attr('cols', 'textAreaWidth');
});
But this is ugly for obvious reasons. I just have no clue how to do this.
Any Ideas?
I have a textarea with the default number of columns, like 10 or 15 or something. I want the text area to shrinkwrap the text inside on blur. Does anyone know how to do this?
So far I have
$('textarea').live('blur', function() {
var textAreaWidth = $(this).val().length;
$(this).attr('cols', 'textAreaWidth');
});
But this is ugly for obvious reasons. I just have no clue how to do this.
Any Ideas?
Share Improve this question asked Apr 12, 2012 at 6:01 mharris7190mharris7190 1,3743 gold badges21 silver badges36 bronze badges3 Answers
Reset to default 2if you want to only decrease the size you should check that if the value is less than, say 20 and then decrease the number of columns. try this:
<textarea rows="2" cols="20"></textarea>
$('textarea').on('blur', function() {
var textAreaWidth = $(this).val().length;
if (textAreaWidth < 20) {
$(this).attr('cols', textAreaWidth);
}
$('textarea').on('keypress', function() {
$(this).attr('cols', '20');
});
});
http://jsfiddle/4bccm/
Remove the single quotes from around 'textAreaWidth' in
$(this).attr('cols', 'textAreaWidth');
This is a variable and doesn't need quotes around it.
$('textarea').bind('blur keyup', function() {
$(this).attr('cols', $(this).val().length);
});
This makes it shrink or grow as you type/blur.
本文标签: javascriptjquery how to decrease number of cols in textarea based on contentStack Overflow
版权声明:本文标题:javascript - jquery how to decrease number of cols in textarea based on content - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745471514a2659771.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论