Skip to content

Instantly share code, notes, and snippets.

@kulakowka
Created April 10, 2014 13:41
Show Gist options
  • Save kulakowka/10383467 to your computer and use it in GitHub Desktop.
Save kulakowka/10383467 to your computer and use it in GitHub Desktop.
Jquery Truncate text in div + show more link
/**
* <div class="text">Some text with <b>html</b>... Any length.</div>
*
*/
$('.text').each(function(){
var length = 5;
var details = $(this);
var original_html = details.html();
var original_text = details.text();
var truncated_text = $.trim(original_text).substring(0, length).split(" ").slice(0, -1).join(" ") + " ";
var show_more = $('<a href="#more">more</a>');
show_more.on('click', function(){
details.html(original_html);
return false;
})
details.html(truncated_text);
details.append(show_more);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment