Jump to content

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

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.