Register  |  Login



Question

Status: Closed Points: 75 Time: 06:47 - Aug 07, 2006  

jgivoni

How to determine whether element has scrollbar

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

Answer Discussion
Answer Summaries

 

Q&A System for Websites and Corporate Collaboration

Advertisement

  • Generates significant organic traffic for websites
  • Saves companies money, resources, and time

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

jgivoni

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

jgivoni

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.

jgivoni

Date:: Nov 20, 2006

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...

jgivoni

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:

jgivoni: 75

You're welcome however to comment or give additional information or if you wish, you have the ability to write an Answer Summary for the Summary 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?

jgivoni

Date:: Feb 11, 2007

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 summary.


Login to rate this summary: Good  |  Bad

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?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top JavaScript Experts

View More

Rank

Expert

Points

1.

nidhi

665

2.

rcastagna

505

3.

jgivoni

428

4.

xarcus

280

5.

theDude

200

6.

redcharcoal

125

7.

roger.berbel

125

8.

Anpanman

87

9.

john2

80

10.

joxley

75

Become an Expert

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! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es