ebolt007 Posted February 8, 2012 Share Posted February 8, 2012 I have the following code for my site, allowing it to only show 300 characters, if there is more than 300 then a [...] More link appears that you can click on to display the rest of the text. My problem is, if someone types one character, then hits enter, they can make a really long piece of text vertically, and I want to stop this by having the following code modified to see line breaks or "\n" and if there are 2 line breaks, then it does the same thing as with 300 characters. How would I set this up tho? $(document).ready(function() { var showChar = 300; var ellipsestext = "..."; var moretext = "more"; var lesstext = "less"; $('.post2').each(function() { var content = $(this).html(); if(content.length > showChar) { var c = content.substr(0, showChar); var h = content.substr(showChar-1, content.length - showChar); var html = c + '<span class="postellipses">' + ellipsestext+ ' </span><span class="morecontent"><span>' + h + '</span> <a href="" class="postlink">' + moretext + '</a></span>'; $(this).html(html); } }); $(".postlink").click(function(){ if($(this).hasClass("less")) { $(this).removeClass("less"); $(this).html(moretext); } else { $(this).addClass("less"); $(this).html(lesstext); } $(this).parent().prev().toggle(); $(this).prev().toggle(); return false; }); }); and my div is <div class="comment2 post2"> A bunch of text in here. Line breaks g e t really long </div> Quote Link to comment https://forums.phpfreaks.com/topic/256685-dont-show-text-with-line-breaks/ 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.