Jump to content

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();    
	});
Edited by dpiearcy
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.