0

I'm currently attempting to make a div with text content on a web page with a "see more" button that will allow the box to expand and un-hide the overflow content. However I only want the "see more" to be visible if there is actually overflow.

Is there some way, in JavaScript, or otherwise that I can detect this?

See: http://jsfiddle.net/AJ277/

0

2 Answers 2

3
if (element.scrollHeight > element.offsetHeight) {
    alert('Overflow!');
}

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

2

You could simply add a wrapper around the text and compare the wrapper height to the parent height.

FIDDLE

JS

if ($('.wrapper').height() > $('#text').height()) {
    //do stuff
}
else { 
    //do other stuff like hide buttons 
}

HTML

<div id="text">
    <div class="wrapper">Lorem ipsum dolor sit amet, consectetur adipiscing elit. In in rutrum lorem. Donec eget turpis arcu. Fusce at neque libero. Nam sed risus velit. Fusce eleifend leo ac laoreet dapibus. Morbi vel tincidunt tortor. Vivamus placerat elit nec mi pulvinar, vitae volutpat nisi tempus.</div>
</div>
<button id="show">show more (only show this button if there is overflow)</button>
<button id="hide" style="display: none">hide</button>

5 Comments

Your fiddle doesn't work. You just disabled the click events, so the button is still there and when you resize it down then the buttons are useless.
@bjb568 Fiddle works, you just need to modify the amount of text to see different result.
No… It doesn't. The buttons don't disappear (or reappear) when you resize your window.
@bjb568, If you remove enough text, the button dissapears. But unlike your post, it doesn't check on browser resize.
People resize their windows. You can't just tell all users to not resize their windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.