Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the actual text that is displayed in a DIV when using CSS style overflow: hidden?

I have the following content DIV on my page, which displays dynamic text:

<div id="someContent">
</div>

It uses the following CSS to cut off additional text:

#someContent {
    height: 200px;
    width: 200px;
    overflow: hidden;
}

If I load this text into the DIV:

"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent aliquam, justo convallis luctus rutrum, erat nulla fermentum diam, at nonummy quam ante ac quam. Maecenas urna purus, fermentum id, molestie in, commodo porttitor, felis. Nam blandit quam ut lacus. Quisque ornare risus quis ligula. Phasellus tristique purus a augue condimentum adipiscing. Aenean sagittis. Etiam leo pede, rhoncus venenatis, tristique in, vulputate at, odio. Donec et ipsum et sapien vehicula nonummy. Suspendisse potenti."

...the CSS causes only the following text to display in the DIV:

"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent aliquam, justo convallis luctus rutrum, erat nulla fermentum diam, at nonummy quam ante ac quam. Maecenas urna purus, fermentum id, molestie in, commodo porttitor, felis. Nam blandit quam ut lacus. Quisque ornare risus quis ligula. Phasellus tristique purus a augue"

This works as expected.

However, I was wondering if there is some way that I can access the displayed text using JavaScript. When I try to access the innerHTML property of the DIV, it returns the entire text originally loaded into the DIV.

<script type="text/javascript">
    alert(document.getElementById("mainArticleContent").innerHTML);
</script>

My end goal is to replace the last word in the trimmed content with ellipsis ("..."). I figured I could do this in JavaScript so that it will display in all browsers, not just IE, as with CSS property text-overflow: hidden.

Any ideas? Is this possible?

like image 925
Eric Belair Avatar asked Apr 22 '09 13:04

Eric Belair


People also ask

How do you handle text-overflow in CSS?

A text-overflow property in CSS is used to specify that some text has overflown and hidden from view. The white-space property must be set to nowrap and the overflow property must be set to hidden. The overflowing content can be clipped, display an ellipsis ('…'), or display a custom string.

How do I display text inside a div?

Using CSS, you can center text in a div in multiple ways. The most common way is to use the text-align property to center text horizontally. Another way is to use the line-height and vertical-align properties. The last way exclusively applies to flex items and requires the justify-content and align-items properties.

How do I stop div text from overflowing?

To fix this problem, avoid using vw for your max-width and use a max-width of 100% instead. This will transfer the width of the parent container to the width of the child container.


3 Answers

I don't think what you're asking to do is possible with javascript.

Alternatively, why not truncate the text at a specific character count server-side? You probably won't have the kind of control you want over the size of the div displayed on the page depending on the font you've selected but it should solve your problem.

Edit:

Take a look at this to get automatic CSS ellipsis. There are a few caveats and it looks pretty browser-specific but this is another method of doing what you want.

like image 194
Dave L Avatar answered Nov 03 '22 16:11

Dave L


This site explains a technique that may be useful for getting the displayed portion of text. I'm not sure how accurate this method would be, but I suspect it'd get you pretty close to what you want: http://www.ruzee.com/blog/2007/08/ellipsis-or-truncate-with-dots-via-javascript

like image 44
kbosak Avatar answered Nov 03 '22 14:11

kbosak


Here's a quick thought using jQuery:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    var innerText = $('#someContent').text();
    var textArray = innerText.split('');
    var formatted = '';
    for(var i in textArray) {
        formatted += '<span>' + textArray[i] + '</span>';
    }
    var heightOfContainer = $('#someContent').height();
    $('#someContent').html(formatted);
    var clipped = '';
    $('#someContent span').each(function(){
        if ($(this).position().top < heightOfContainer) {
            clipped += $(this).text();
        } else {
            return false;
        }
    });
    clipped += '...';
    $('#someContent').html(clipped);
});
</script>
like image 27
Jerph Avatar answered Nov 03 '22 15:11

Jerph