Skip to content

Instantly share code, notes, and snippets.

@putrasurya
Created April 28, 2015 11:17
Show Gist options
  • Save putrasurya/41698bb208c338db0ffa to your computer and use it in GitHub Desktop.
Save putrasurya/41698bb208c338db0ffa to your computer and use it in GitHub Desktop.
Auto Resize Textarea jQuery plugin
/**
* Auto Resize Textarea jQuery plugin
*
* Created by putra.surya.h60@gmail.com | Twitter @PutraSoerya9
*
*/
(function($){
var AutoResize = function () {
var o, el,
_ = this;
_.init = function (elm, opt) {
o = opt;
el = elm;
el.ready(_.autoresize({}));
el.bind('keyup.AutoResize', _.autoresize);
}
_.autoresize = function (e) {
var content = el.val(),
splitRes = content.split("\n");
if (splitRes.length > o.line-1) {
el.css('height', o.line_height*splitRes.length);
}
};
};
$.fn.AutoResizeTextarea = function(opt) {
var o = $.extend({
line: 3,
line_height: 21 /* line height css style of this textarea */
}, opt);
return $(this).each(function(){
var elm = $(this);
if (!elm.is("textarea")) return;
var autoResize = (new AutoResize).init(elm, o);
$(this).data('AutoResizeTextarea', autoResize);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment