Skip to content

Instantly share code, notes, and snippets.

@kijtra
Created June 19, 2014 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kijtra/d3f63f3f810ba50edc46 to your computer and use it in GitHub Desktop.
Save kijtra/d3f63f3f810ba50edc46 to your computer and use it in GitHub Desktop.
[JavaScript] テキストエリアの高さを自動調節するやつ
(function($){
var update = function(obj, min){
var br = obj.val().match(/(\r\n|\n)/gm);
if (br) {
if (br.length >= min) {
obj.prop('rows', br.length + 1);
}
} else {
obj.prop('rows', min);
}
};
this.each(function(){
var t = $(this),
min = Number(t.prop('rows') || 1);
t.data('ah-minrow', min);
if (this.value) {
update(t, min);
}
t.on('keyup change cut paste', function(e){
var code = (e.keyCode || e.which), type = e.type;
if (13 === code || 8 === code || 46 === code || (e.ctrlKey && 90 === code)) {
update(t, min);
} else if ('paste' === type || 'cut' === type) {
var th = $(this);
setTimeout(function() {
update(th, th.data('ah-minrow'));
}, 100);
}
});
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment