Created
April 28, 2015 11:17
-
-
Save putrasurya/41698bb208c338db0ffa to your computer and use it in GitHub Desktop.
Auto Resize Textarea jQuery plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Auto Resize Textarea jQuery plugin | |
* | |
* Created by [email protected] | 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