1

Pretty sure its a duplicate but I dont find any answer in other post "relative" plus I'm pretty new to Js so dont be too harsh pls :/

I'm creating a simple function in Js/jQuery for resuming too long text. I've done it; it works but now I want to use it with a variable (the number of characters) I specifically set when I call it.

Something like resume(30) while mean if the text is higher than 30 characters resume it after them.

Here Is what I've got so far : ( You can change nbChar by a value )

var nbChar;
function resume(nbChar) {
    $('.resume').each(function () {
        var text = $(this).text();
        if (text.length > nbChar) {
            var less = text.slice(0, nbChar) + '<span class="showMore">...</span>';
            $(this).html(less);

            $(this).data('content', text + '<br/><br/><span class="showLess"> Cacher</span>')
            $(this).data('less', less)
        }
    });
});

Here is a Fiddle if this could help you answer me.

Any help would be gladly accepted, thank you!

1 Answer 1

4

Your code works fine, after fixing:

  • <div class=".resume"> should be <div class="resume">
  • Line 22 }); should be }
  • include jQuery library in jsfiddle

https://jsfiddle.net/0yps2b90/1/

Sign up to request clarification or add additional context in comments.

1 Comment

Hum ok so it is an internal problem. Thank you anyway :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.