Reputation: 5271
How to count characters rather than text? I limited 30 words, how to limit 30 characters Thanks a lot.
function excerpt(str, nwords) {
var words = str.split(' ');
words.splice(nwords, words.length - 1);
return words.join(' ') + '…';
}
var $div = $('.container');
$div.each(function() {
var theExcerpt = excerpt($(this).text(), 30);
$(this).data('html', $(this).html()).html( theExcerpt );
});
$('span').click(function() {
var isHidden = $(this).text() == 'Show';
var $div = $(this).prev();
var theExcerpt = excerpt($div.text(), 30);
$div.html( isHidden ? $div.data('html') : theExcerpt);
$(this).remove();
});
Herer ist the snippet http://jsfiddle.net/Nh4K2/
Upvotes: 0
Views: 106
Reputation: 664356
function excerpt(text, len) {
return text.substring(0, len)+"…";
}
Upvotes: 2