dpiearcy Posted May 29, 2013 Share Posted May 29, 2013 (edited) 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 May 29, 2013 by dpiearcy Quote Link to comment https://forums.phpfreaks.com/topic/278534-truncate-revisited/ Share on other sites More sharing options...
nogray Posted May 29, 2013 Share Posted May 29, 2013 Without seeing the actual text, it's hard to debug. But it seems you are losing 10 characters when you slice your strings (slice to 90 and start from 100). A much easier solution would be to use CSS text-overflow. Quote Link to comment https://forums.phpfreaks.com/topic/278534-truncate-revisited/#findComment-1432899 Share on other sites More sharing options...
dpiearcy Posted May 29, 2013 Author Share Posted May 29, 2013 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). Quote Link to comment https://forums.phpfreaks.com/topic/278534-truncate-revisited/#findComment-1432943 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.