Jump to content

Don't show text with line breaks


ebolt007

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/256685-dont-show-text-with-line-breaks/
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.