Select Categories Below
Advertisement
Start Your Own Q&A Site
Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.
Status: Closed Points: 75 Time: 06:47 - Aug 07, 2006
jgivoni
I have an element like this: <div style="height: 100px; overflow: auto">Variable content</div> 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
Categories:
Add Categories
When adding more than one category, separate them with commas.
Anpanman
Date:: Aug 18, 2006
Time:: 01:31
Did you find an answer to this? As far as I know it is not possible to determine.
PeterNZ
Date:: Aug 27, 2006
Time:: 22:13
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
Date:: Aug 28, 2006
Time:: 14:06
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
Date:: Nov 20, 2006
Time:: 15:46
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.
Time:: 16:09
This can be used for various purposes, - for instance to automatically expand a textarea box when it get's full: <textarea style="width: 200px height: 50px; overflow: auto" onkeyup="textarea_size(this)"></textarea> <script> function textarea_size(elem) { w1 = elem.style.width.replace(/[^\d]/g, ""); w2 = elem.clientWidth; if ((w1 - w2) > 10) { h = elem.style.height.replace(/[^\d]/g, ""); elem.style.height = parseInt(parseInt(h) + 8) + "px"; textarea_size(elem); } } </script> 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...
Date:: Feb 11, 2007
Time:: 13:49
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.
Question Answered
This question has been closed, and points have been rewarded to the following experts:
You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.
Answer this Question
New User
Email:
Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address
Registered User
Username:
Password:
Forgot Your Password?
Enter your email address below and we will resend your login information to you.
Login Information Sent
Time:: 14:11
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.
Click here to see the Answer Discussion that preceded this tutorial.
Login to rate this tutorial: Good | Bad
name
Date:: Dec 08, 2009
Time:: 03:32
thanks for answer :)
Questions
Hi is there a way to retrieve the loop count in XSLT
how do repair this problem: every times i opened my laptop it will suddenly n...
How i get the hp pavilion dv6-3054tx laptop bottom base assembly as a sparepa...
I don't have AOL browser but privacy scan shows hundreds of files/cookies fro...
wireless keyboard has stopped working
need validation controller for textbox in java
You have 100 characters to use
Rank
Expert
Points
1.
10279
2.
6493
3.
5596
4.
4848
5.
3487
6.
2840
7.
2770
8.
2303
9.
1820
10.
917
Register today to share your knowledge with the community and be recognized and rewarded for your contributions.
Register Here
"Psst, Quomon is a great site. Pass it on." Tell a Friend | Link To Us | Save to Delicious | Digg it
Language Options
English:
Español:
Sponsors
Questions and Answers Software