Implementing 'Read More - Read Less' in Javascript

Use-Cases of this Tutorial

  • When a text is too long to fit, you can cut it short, and add a "See More" button. Clicking on this button will show the complete text.

A good way to reduce a long text to a shorter one is through "Read More - Read Less" functionality. This can be implemented with Javascript.

Demo

" At the end of the day, you are solely responsible for your success and your failure. And the sooner you realize that, you accept that, and integrate that into your work ethic, you will start being successful. As long as you blame others for the reason you aren't where you want to be, you will always be a failure. "....Read More

How Does it Work ?

Assume you have a long text of the form :

<div>At the end of the day, you are solely responsible for your success and 
your failure. And the sooner you realize that, you accept that, and integrate that into your work 
ethic, you will start being successful. As long as you blame others for the reason 
you aren't where you want to be, you will always be a failure.</div>

The solution is to divide this long text into 4 inline elements :

<div>
	<span class="short-text">At the end of the day, you are solely responsible for your success
	 and your failure. And the sooner you realize that, you accept that, and integr</span>
	<span class="long-text">ate that into your work ethic, you will start being successful. As 
	long as you blame others for the reason you aren't where you want to be, you will 
	always be a failure.</span>
	<span class="text-dots">....</span>
	<span class="show-more-button" data-more="0">Read More</span>
</div>

Initially only the span short-text would be seen, and long-text would be hidden.

.long-text {
	display: none
}

On clicking show-more-button, long-text would be seen, thereby showing the complete text. Now that complete text is seen, the text in show-more-button would be set to "Read Less".

A click again on show-more-button would hide long-text. show-more-button would hold the text "Read More" now.

Breaking the Long Text into inline Elements

// Long text
var text = 'At the end of the day, you are solely responsible for your success and your failure. And the sooner you realize that, you accept that, and integrate that into your work ethic, you will start being successful. As long as you blame others for the reason you are not where you want to be, you will always be a failure.';

// Character limit after which "Read More" will be seen
var char_limit = 150;

if(text.length < char_limit)
	console.log( '<div> ' + text + '</div>' );
else
	console.log( '<div><span class="short-text">' + text.substr
I'm available for freelancing
If you've liked this article, you'll probably like my code too.

From short code snippets to complete web applications, I code in Javascript & PHP
Low Prices from $20, Fast Delivery Time
I'm available for freelancing
If you've liked this article, you'll probably like my code too.

From short code snippets to complete web applications, I code in Javascript & PHP
Low Prices from $20, Fast Delivery Time