javascript - Why does .setAttribute not work with jQuery -
$(function () { $('textarea').setattribute('style', 'height:' + ($('textarea').scrollheight) + 'px;'); });
<textarea> very, long preloading text, mean vvveeerrryyy, vvveeerrryyy long one! </textarea>
what doing wrong, please? double checked it, can't find wrong
- its
attr()
. - change
$('textarea').scrollheight
$('textarea')[0].scrollheight
.because return undefined setattribute()
not defined function in jquery. , don't forget add jquery library in html.
$(function () { $('textarea').attr('style', 'height:' + ($('textarea')[0].scrollheight) + 'px;'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea>this very, long preloading text, mean vvveeerrryyy, vvveeerrryyy long one!</textarea>
Comments
Post a Comment