Voted Best Answer
Feb 11, 2007 - 10:11 AM
elem.clientHeight < elem.scrollHeight
where elem is the block element (div, textarea, etc.) you want to check.
If the above comparison is true, then the block element has a vertical scrollbar. If you want to check for horizontal scrollbars, you should replace Height with Width.
Here is a function to demonstrate it:
function has_scrollbar(elem_id)
{
elem = document.getElementById(elem_id);
if (elem.clientHeight < elem.scrollHeight)
alert("The element has a vertical scrollbar!");
else
alert("The element doesn't have a vertical scrollbar.");
}
Call the function with the id of the element to check. It will pop up a javascript alert with the answer.
Here is a function that automatically sizes an element vertically to fit the content:
function autosize(elem_id)
{
elem = document.getElementById(elem_id);
elem.style.height = elem.scrollHeight + "px";
}
In Firefox it will only expand the box - never contract it.
Add New Comment