Created
June 19, 2014 02:13
-
-
Save kijtra/d3f63f3f810ba50edc46 to your computer and use it in GitHub Desktop.
[JavaScript] テキストエリアの高さを自動調節するやつ
This file contains 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
(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