Created
April 10, 2014 13:41
-
-
Save kulakowka/10383467 to your computer and use it in GitHub Desktop.
Jquery Truncate text in div + show more link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* <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