I have paragraph which has more than 500 character. I want to get only initial 100 character and hide rest of it. Also I want to insert \"More\" link next to 100 character.
for everyone who has come here searching for show more... Here is another plug-in http://viralpatel.net/blogs/dynamically-shortened-text-show-more-link-jquery/
It's not a top google result, but I've used the jQuery Expander plugin to great success. It's nice because it doesn't hide anything from search engine robots.
Fiddle: http://jsfiddle.net/iambriansreed/bjdSF/
jQuery:
jQuery(function(){
var minimized_elements = $('p.minimize');
var minimize_character_count = 100;
minimized_elements.each(function(){
var t = $(this).text();
if(t.length < minimize_character_count ) return;
$(this).html(
t.slice(0,minimize_character_count )+'<span>... </span><a href="#" class="more">More</a>'+
'<span style="display:none;">'+ t.slice(minimize_character_count ,t.length)+' <a href="#" class="less">Less</a></span>'
);
});
$('a.more', minimized_elements).click(function(event){
event.preventDefault();
$(this).hide().prev().hide();
$(this).next().show();
});
$('a.less', minimized_elements).click(function(event){
event.preventDefault();
$(this).parent().hide().prev().show().prev().show();
});
});
Thanks to @iambriansreed for his nice function, here's a slight modification to truncate paragraph on line breakes:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
jQuery(function(){
var minimized_elements = $('p.minimize');
var maxLines = 20;
minimized_elements.each(function(){
// var textArr = $(this).text().split(/\n/); // Not supported in IE < 9
var textArr = $(this).html().replace(/\n?<br>/gi,"<br>").split(/<br>/);
var countLines = textArr.length;
if (countLines > maxLines) {
text_less = textArr.slice(0, maxLines).join("<br>");
text_more = textArr.slice(maxLines, countLines).join("<br>");
}
else return;
$(this).html(
text_less + '<span>... </span><a href="#" class="more">More</a>'+
'<span style="display:none;">'+ text_more +' <a href="#" class="less">Less</a></span>'
);
});
$('a.more', minimized_elements).click(function(event){
event.preventDefault();
$(this).hide().prev().hide();
$(this).next().show();
});
$('a.less', minimized_elements).click(function(event){
event.preventDefault();
$(this).parent().hide().prev().show().prev().show();
});
});
</script>
Have you looked at the jQuery Truncator plugin?
It pretty much does exactly what you've described.
It looks like a couple other people beat me to it, but here is what I came up with.
var MORE = "... More...",
LESS = " Less...";
$(function(){
$("p").each(function(){
var $ths = $(this),
txt = $ths.text();
//Clear the text
$ths.text("");
//First 100 chars
$ths.append($("<span>").text(txt.substr(0,100)));
//The rest
$ths.append($("<span>").text(txt.substr(100, txt.length)).hide());
//More link
$ths.append(
$("<a>").text(MORE).click(function(){
var $ths = $(this);
if($ths.text() == MORE){
$ths.prev().show();
$ths.text(LESS);
}
else{
$ths.prev().hide();
$ths.text(MORE);
}
})
);
});
});