Jump to content

Truncate revisited


dpiearcy

Recommended Posts

Ok.  Have it truncating the way I want.  Problem is now when I click "More" I get Less Less Less Less Less Less then the final Less is a link.  The others are not.  This is in the loop I get back from my PHP file using JSONP.  I've looked at it 6 ways to Sunday but can't seem to figure out how to make less less's :-) Odd that only the last one is a link the rest are just text. 

$.each(data, function(index, user){
   var minimized_elements = $('p.minimize');
    minimized_elements.each(function(){    
    var t = $(this).text();        
    if(t.length < 90) return;
        
$(this).html(
t.slice(0,90)+'<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();    
	});
Link to comment
https://forums.phpfreaks.com/topic/278534-truncate-revisited/
Share on other sites

I considered CSS but I want that div to expand and show more when the more link is clicked then retract when the less link is clicked.  Good catch on the 90/100.  I originally started with 100 then moved it back to 90 because I had a text wrap issue I didn't like the looks of.   

 

I guess it would be possible to still style this in CSS but figured this would be easier.  Would still need to use javascript though to change my CSS values.  

 

Not sure what text you are wanting.  The text is coming from MySQL via PHP delivered thanks to JSON and an AJAX call so it varies.  Some post are less than the 90 characters while others are much longer (the row in MySQL is tiny text if I recall so no a varchar which limits me to 144).  

Link to comment
https://forums.phpfreaks.com/topic/278534-truncate-revisited/#findComment-1432943
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.