Создание текстовой области с автоматическим изменением размера
был еще один поток об этом, который я пробовал. Но есть одна проблема:textarea не сжимается при удалении содержимого. Я не могу найти способ уменьшить его до нужного размера - в clientHeight значение возвращается как полный размер textarea, а не его содержание.
код с этой страницы ниже:
function FitToContent(id, maxHeight)
{
var text = id && id.style ? id : document.getElementById(id);
if ( !text )
return;
var adjustedHeight = text.clientHeight;
if ( !maxHeight || maxHeight > adjustedHeight )
{
adjustedHeight = Math.max(text.scrollHeight, adjustedHeight);
if ( maxHeight )
adjustedHeight = Math.min(maxHeight, adjustedHeight);
if ( adjustedHeight > text.clientHeight )
text.style.height = adjustedHeight + "px";
}
}
window.onload = function() {
document.getElementById("ta").onkeyup = function() {
FitToContent( this, 500 )
};
}
Comments