<script type="text/javascript">
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 )
};
}
</script><textarea name="" style="width:440px; height: 21px; overflow-y:hidden;" onkeyup=" FitToContent(this,300)"></textarea>
ส่วนนี้เป็นส่วนของ Text area ที่ต้องการนำสคริปไปใช้ครับ
ตรงเลข 300 นั่น หมายถึงว่า Text area นี้จะเพิ่มความสูงได้มากสุด 300 pixel นะครับ
และ width:440px; height: 21px; คือค่าที่ set ไว้ให้เริ่มต้น กว้าง 440 และ สูง21
แต่พอพิ่มข้อความเกินความกว้างแล้ว สคริปจะเริ่มทำงาน
ลองเอาไปใช้ดู สำหรับคนที่อาจจะต้องการใช้สคริปนี้มาช่วยในการประหยัดเนื้อที่แสดงผลของช่อง text area