10

I am trying to add a link to read more text at the end of a paragraph. I would like this link to display within the paragraph at the end like so:

I want the paragraph to be appended with the ... and the read more link.

For now I don't want the read more link to do anything as I will add my own function to it once I get the link in place. I am just struggling to find a way to get the link to appear like this.

I have been looking at the following jquery script, however this seems to work on a character count and cuts the last word at whatever the character limit is and then doesn't display the way I want it to (the link appears below the paragraph). It also already contains a function for what happens when the link is clicked which due to my lack of ability with jquery, I have been unsuccessful at editing out.

$(function(){ /* to make sure the script runs after page load */
    $('.customer_experience').each(function(event){ /* select all divs with the  customer_experience class */
        var max_length = 250; /* set the max content length before a read more link will be added */

        if($(this).html().length > max_length){ /* check for content length */
            var short_content   = $(this).html().substr(0,max_length); /* split the content in two parts */
            var long_content    = $(this).html().substr(max_length);

            $(this).html(short_content+
                '<a href="#" class="read_more"><br/>read more...</a>'+
                '<span class="more_text" style="display:none;">'+long_content+'</span>'); /* Alter the html to allow the read more functionality */

            $(this).find('a.read_more').click(function(event){ /* find the a.read_more element within the new html and bind the following code to it */
                event.preventDefault(); /* prevent the a from changing the url */
                $(this).parents('.customer_experience').find('.more_text').show(); /* show the .more_text span */
            });
        }
    });
});

Do I need to use jQuery at all to get the result I am looking for? Can this be done with CSS alone? I don't really write jQuery at all so I am a bit lost.

Any help or hints at where I can go with this would be much appreciated.

Edited to add HTML Mark-up

<div class='comments_container'>
    <img src='images/comment-icon.png'/>
    <h1 class='comments_title'>Customer experience</h1>
    <p class='customer_experience'>$customer_exp_one</p>
</div>

The paragraph in question is .customer_experience.

Community
  • 1
  • 1
Richard Bell
  • 405
  • 3
  • 7
  • 21

5 Answers5

21

I hope the below one will help you.

http://jsfiddle.net/X5r8r/1156/

body, input {
    font-family: Calibri, Arial;
    margin: 0px;
    padding: 0px;
}
a {
    color: #0254EB
}
a:visited {
    color: #0254EB
}
#header h2 {
    color: white;
    background-color: #00A1E6;
    margin: 0px;
    padding: 5px;
}
.comment {
    width: 400px;
    background-color: #f0f0f0;
    margin: 10px;
}
a.morelink {
    text-decoration: none;
    outline: none;
}
.morecontent span {
    display: none;
}
Prakash Pazhanisamy
  • 997
  • 1
  • 15
  • 25
vinothini
  • 2,606
  • 4
  • 27
  • 42
  • This is very helpful. Thank you. I do not want the div to expand when the more link is clicked though. Can this be edited out of the jquery? I just want the link to display so that I can add my own function to it. I am going to add a jquery pop up window to display the text in full. – Richard Bell Apr 17 '13 at 11:37
  • Hi Richard Bell i updated the fiddle please have a look on http://jsfiddle.net/X5r8r/1158/ – vinothini Apr 17 '13 at 12:34
  • Excellent! Your answer best fits my requirements. I am going to attempt to combine the solution presented by lordvlad with your solution so that the end word is not cut. Thank you very much for your time and effort. – Richard Bell Apr 17 '13 at 13:39
  • @Richard Bell, I notice this does not work if the text in the read more section has other elements in it. Is there a good way to get around that? – Taylor C. White Jul 15 '15 at 06:06
4

For cutting off paragraphs. This script checks the paragraph count, not the word count.

JS (With Jquery)

$(document).ready(function() {

    /* Count of paragraphs shown */
    var cutCount = 2;

    $("#testID p").each(function (i) {
        i++;
        if(i == cutCount) {
            $(this).append(' <a href="javascript:void(1)" onclick="$(\'#testID p\').show(); $(this).hide()">Read more</b>')   
        }
        if(i > cutCount) {
           $(this).hide();
        }
    });

});

HTML

<div id="testID">

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam egestas lobortis mi, quis convallis arcu interdum id. Nunc velit justo, congue ac vestibulum eu, sodales sed nulla. Nam semper egestas nunc, placerat suscipit lorem fringilla sit amet. Etiam id metus quis tellus luctus rhoncu</p>

<p>Donec euismod, dui aliquam vestibulum rutrum, urna ipsum luctus justo, in eleifend tellus ligula et elit. Morbi gravida lacinia magna quis faucibus. Duis arcu ligula, euismod sed ornare quis, posuere ac quam. Sed orci metus, pretium ut blandit sed, ullamcorper in eros. Mauris at euismod diam. Quisque auctor congue erat et varius. Nulla odio orci, convallis non fringilla vel.</p>

<p>Curabitur a velit eu lacus vestibulum vehicula. Proin mi velit, tempor quis convallis vulputate, adip</p>

<p>Nullam eget pulvinar arcu. Nam tellus tortor, luctus non rutrum eget, condimentum ac lectus. Nulla diam tellus, aliquet non auctor nec, bibendum nec orci. Proin rhoncus sodales sapien, sit amet eleifend erat sagittis quis. Ut et urna et erat venenatis convallis at sit amet sem. Quisque tempus sodales ornare. Maecenas eget nisi et ligula facilisis dictum.</p>

<p>Etiam lectus dolor, tincidunt a ultricies et, vehicula ut lectus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean bibendum suscipit nunc sit amet ultrices. Praesent nunc velit, dignissim eget tincidunt non, varius at velit. Aliquam ac interdum lectus. Nulla a ante lacinia arcu suscipit suscipit a nec mauris. In sed tempor massa. Donec luctus dapibus dolor, vel mattis justo pulvinar eget. Pellentesque scelerisque dolor ut erat cursus ornare. In in nunc et tellus consequat convallis id sed est. Integer dictum fermentum tempus. Ut lobortis ante vel ante condimentum lacinia. Donec malesuada pretium sapien, et euismod turpis faucibus at. Duis vel turpis ultrices enim viverra vestibulum vitae sed sapien.</p>

</div>

Example http://jsfiddle.net/9bXQh/2/

S.Visser
  • 4,645
  • 1
  • 22
  • 43
2

you can cut at spaces with something like this:

$(this).html().substr(0, $(this).html().indexOf(" ", max_length))
lordvlad
  • 5,200
  • 1
  • 24
  • 44
1

I did a web search and found this plugin: http://dotdotdot.frebsite.nl/.

I guess that doesn't really answer your question, just points you in the direction of a possible solution. It seems this plugin allows you to do what you want: truncate the text when it exceeds a fixed-height container, add a 'read more' link, and bind your own custom function to the 'read more' link.

Derek Henderson
  • 9,388
  • 4
  • 42
  • 71
0

Click the buttons below to show and hide another element via class changes:

.collapse hides content

.collapsing is applied during transitions

.collapse.show shows content You can use a link with the href attribute, or a button with the data-target attribute. In both cases, the data-toggle="collapse" is required. HTML

  <div class="container">

     <div class="row justify-content-center">
      <div class="col-md-4">
        <div id="parent">
         <p class="collapse" id="collapseParent">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc     </p>
         <a class="collapsed" data-toggle="collapse" href="#collapseParent" aria-expanded="false" aria-controls="collapseParent"></a>
   </div>
 </div> 

CSS

#parent {
   font-size: 14px;
   line-height: 1.5;
}

#parent p.collapse:not(.show) {
   height: 42px !important;
   overflow: hidden;
   display: -webkit-box;
   -webkit-line-clamp: 2;
   -webkit-box-orient: vertical;  
  }

#parent p {
    min-height: 42px !important;
}

 #parent a.collapsed:after  {
     content: 'Read More';
  }

  #parent a:not(.collapsed):after {
      content: 'Read Less';
  }

CodePen Example

Kondal
  • 2,870
  • 5
  • 26
  • 40