Skip to content

Instantly share code, notes, and snippets.

@quyen91
Created April 22, 2016 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quyen91/fdcca9cce3fffc5247fad3b51e9c1eb6 to your computer and use it in GitHub Desktop.
Save quyen91/fdcca9cce3fffc5247fad3b51e9c1eb6 to your computer and use it in GitHub Desktop.
$(function(){ /* to make sure the script runs after page load */
$('.item').each(function(event){ /* select all divs with the item class */
var max_length = 150; /* set the max content length before a read more link will be added */
if($(this).html().length > max_length){ /* check for content length */
var short_content = $(this).html().substr(0,max_length); /* split the content in two parts */
var long_content = $(this).html().substr(max_length);
$(this).html(short_content+
'<a href="#" class="read_more"><br/>Read More</a>'+
'<span class="more_text" style="display:none;">'+long_content+'</span>'); /* Alter the html to allow the read more functionality */
$(this).find('a.read_more').click(function(event){ /* find the a.read_more element within the new html and bind the following code to it */
event.preventDefault(); /* prevent the a from changing the url */
$(this).hide(); /* hide the read more button */
$(this).parents('.item').find('.more_text').show(); /* show the .more_text span */
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment