How to determine whether element has scrollbar
I have an element like this:
Variable content
It gives me a box of 100 pixels height with some text inside. If the text is too big for the box, it shows a scrollbar to scroll through the text.
I need to know from javascript if the box has got the scrollbar in a certain situation.
That is to say, is there a function to let me know if the box has been overflowed?
Thanks,
Jakob
Status:
Open Aug 07, 2006 - 03:47 AM
JavaScript, css, web development, html, scrollbar, DHTML
7answers
Voted Best Answer
Feb 11, 2007 - 10:11 AM
The simple solution:
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.
Answers
Aug 17, 2006 - 10:31 PM
Did you find an answer to this?
As far as I know it is not possible to determine.
Aug 27, 2006 - 07:13 PM
What I wonder is, how do you handle the width of your element? Why don't you set a width attribute lets say of 50 px and then check if the content of the box is greater than 50px. Then you know it has scrollbars.
Don't know if this helps you.
Cheers
Peter
Aug 28, 2006 - 11:06 AM
Peter,
Your idea is actually what I have been using since I didn't get an answer to this question.
But actually I haven't checked if a browser will report the width or height to more that the actual width or height of the box even though it has overflowed (showing scrollbars).
What I do now is not to set "overflow: auto" and then just check if box.clientHeight has exceeded the maximun I choose. If it has, I remove the last item again. In IE this only works if the height of the box has been set in css. Otherwise it doesn't report the clientHeight property.
Peter, how would you suggest to "check if the content of the box is greater than 50px"?
Jakob
Nov 20, 2006 - 11:46 AM
I finally found a way to check if an element has a scrollbar.
It is actually quite simple, since if there is a scrollbar it will take up some space inside the element.
If the elem.style.width is greater than elem.clientWidth (about 14-16 px difference), then it suggest that the element has a vertical scrollbar.
Nov 20, 2006 - 12:09 PM
This can be used for various purposes, - for instance to automatically expand a textarea box when it get's full:
!
This example is quickly written and briefly tested on FF2.0 and IE 6. There is most probably room for improvement.
For instance, it is assumed that the width and height is set in pixels and that the scrollbar has not been modified to be narrower that 10px...
Feb 11, 2007 - 09:49 AM
Since my last answer (to my own question) I've found a much better solution.
I've discovered the element property "scrollHeight"!
Read more about it in the summary.
Answer this question
Share Your Own Experience & Expertise
We look to ensure that every question is answered by the best people with relevant expertise and experience, the best answers include multiple perspectives. Do you have relevant expertise or experience to contribute your answer to any of these commonly asked questions?
Add New Comment