[javascript] textareaの縦幅を自動で変更する
textareaの縦幅を自動で変更するには以下のコードを実装する
function fitTextareaHeight(textareaId) {
var textarea = document.getElementById(textareaId);
textarea.style.height = 'auto'; // Reset height to auto to calculate new height
textarea.style.height = textarea.scrollHeight + 'px'; // Set height to scroll height
}
// Example usage:
// Assuming you have a textarea with id="myTextarea"
// You can call the fitTextareaHeight function whenever the textarea content changes
document.getElementById('myTextarea').addEventListener('input', function() {
fitTextareaHeight('myTextarea');
});
// Call it initially to set the initial height
fitTextareaHeight('myTextarea');