Register  |  Login

Question Details    

   Question

Time: 06:47 - Aug 07, 2006     Asked by: jgivoni      Status: Answered      Points: 75   

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

Ask a New Question

Become a Quomon Expert

Current Categories

 

Other Questions Needing Answers


   

Answer Discussion
Answer Discussion
Answer Summaries
Answer Summary
 
Did you find an answer to this?
As far as I know it is not possible to determine.

Expert:

Anpanman

Date:

Aug 18, 2006

Time:

01:31

 

Votes: Good (0) | Bad (0)
Login to rate this answer

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

Expert:

PeterNZ

Date:

Aug 27, 2006

Time:

22:13

 

Votes: Good (0) | Bad (0)
Login to rate this answer

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

Expert:

jgivoni

Date:

Aug 28, 2006

Time:

14:06

 

Votes: Good (0) | Bad (0)
Login to rate this answer

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.

Expert:

jgivoni

Date:

Nov 20, 2006

Time:

15:46

 

Votes: Good (0) | Bad (0)
Login to rate this answer

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

Expert:

jgivoni

Date:

Nov 20, 2006

Time:

16:09

 

Votes: Good (0) | Bad (0)
Login to rate this answer

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.

Expert:

jgivoni

Date:

Feb 11, 2007

Time:

13:49

 

Votes: Good (0) | Bad (0)
Login to rate this answer

Question Answered

This question has been answered, 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 this question by clicking on the "Answer Summaries" Tab.


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.

Expert:

jgivoni

Date:

Feb 11, 2007

Time:

14:11

 
 

Login to rate this summary: Good  |  Bad

Respond to 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:


Forgotten Password

 

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:


Forgotten Password

   

"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it


All Questions


Language Options

English:

www.quomon.com

Español:

www.quomon.es

Quomon Blog

blog.quomon.com

Sponsors

Questions and Answers Software
Real Estate Postcards
Marketing Fulfillment