PHP/jQuery Shorten String And Show More On Click
I have a dynamic page that loads user content from a database. If the user's post is greater than 50 characters, I echo this code: $data['string'] = substr($data['string'], 49, 50)
Solution 1:
If you output all the text from PHP, you can use this Fiddle to make things interesting.
Fiddle: http://jsfiddle.net/iambriansreed/bjdSF/
jQuery(function(){
var minimized_elements = $('p.minimize');
minimized_elements.each(function(){
var t = $(this).text();
if(t.length < 100) return;
$(this).html(
t.slice(0,100)+'<span>... </span><a href="#" class="more">More</a>'+
'<span style="display:none;">'+ t.slice(100,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();
});
});
Solution 2:
Use the JQuery Shorten Plugin it is easy to implement and here is it's demo. The download zip contains samples as well.
Hope it helps
Post a Comment for "PHP/jQuery Shorten String And Show More On Click"