JavaScript How to add “read more” link with many paragraphs in jquery?

I have a content DIV with many paragraphs. This is how its' markup look,

<div class="more">
    <p>Based on a study of its skull, scientists say that an extinct one-tonne relative of the guinea pig probably used its 30cm incisors like tusks. Based on a study of its skull, scientists say that an extinct one-tonne relative of the guinea pig probably used its 30cm incisors like tusks. </p>
    <p>That an extinct one-tonne relative of the ncisors like tusks. Based on a study of its skull, scientists say that an extinct one-tonne relative of the guinea pig probably used its 30cm incisors like tusks. </p>
    <p>Extinct one-tonne relative of the guinea pig probably used its 30cm incisors like tusks. Based on a study of its skull, scientists say that an extinct one-tonne relative of the guinea pig probably used its 30cm incisors like tusks. </p>
</div>

Now I need to add a link to "read more" once the characters reach to 300 of <div class="more">. And also I would like to display this link as a toggle.

Just I tried it with jquery, but I couldn't figure it out properly.

This is my jquery -

var showChar = 300;
var ellipsestext = "...";
var moretext = "more";
var lesstext = "less";
$('.more').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="moreelipses">'+ellipsestext+'</span>&nbsp;<span class="morecontent"><span>' + h + '</span>&nbsp;&nbsp;<a target='_blank' href="" class="morelink">'+moretext+'</a></span>';

        $(this).html(html);
    }

});

$(".morelink").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;
});

JS FIDDLE with current jquery

Hope somebody may help me out. Thank you.

Answer:1

I would like to make a chrome extension that simply serve static content from a defined directory. As usual the static directory will be in the extension repository. I would like the extension to ...

I would like to make a chrome extension that simply serve static content from a defined directory. As usual the static directory will be in the extension repository. I would like the extension to ...

  1. chrome extension static id

I'm trying to make a GET request through jQuery to the Mailchimp API. It seems though my custom header is not correctly set as I get a Your request did not include an API key. error. It works fine if ...

I'm trying to make a GET request through jQuery to the Mailchimp API. It seems though my custom header is not correctly set as I get a Your request did not include an API key. error. It works fine if ...

I see a strange behavior when redirecting to a URL with Backbone router. If I try to redirect to: //myserver/component_issues/index?id=org.codehaus.sonar:sonarqube ... everything is fine, but I get ...

I see a strange behavior when redirecting to a URL with Backbone router. If I try to redirect to: //myserver/component_issues/index?id=org.codehaus.sonar:sonarqube ... everything is fine, but I get ...

I want to return data from a Service function and inject it into a controller via the routeProvider, but only once the data has been fully loaded. app.js: .when('/sources', { templateUrl:...

I want to return data from a Service function and inject it into a controller via the routeProvider, but only once the data has been fully loaded. app.js: .when('/sources', { templateUrl:...

  1. angular return data from subscribe
  2. angular return data from observable
  3. angular return data from dialog
  4. angular return data from service to component
  5. angular return data from modal
  6. angular return data from service
  7. angular return data from component
  8. angular return data from promise
  9. angular return data
  10. angular return data from post
  11. angular return data from api
  12. angular return data from function
  13. angular return data from http
  14. angular return data from http call
  15. angular material return data from dialog
  16. angular 6 return data from service
  17. angular 6 return data from subscribe
  18. angular httpclient return data
  19. angular 7 return data from subscribe
  20. angular 6 return data from observable