How to implement Read More and Read Less with dotdotdot?

Joe

Using the jQuery dotdotdot plugin, I would like to have both a More and Less button to show and hide entire content of a <div> when there is a lot of text to display. The More button is working just fine, but I haven't yet figured out a way to return the <div> to it's original display. Note that this is not just about how to use dotdotdot to expand a truncated string because it incorporates the Less button re-truncate a long string.

Here is my code:

$(function() {
    $("div.ellipsis-text").dotdotdot({
        after: 'a.more',
        callback: dotdotdotCallback
    });
    $("div.ellipsis-text").find("a").click(function() {
        if ($(this).text() == "More") {
            var div = $(this).closest('div.ellipsis-text');
            div.trigger('destroy').find('a.more').hide();
            div.css('max-height', '');
            $("a.less", div).show();
        }
        else {
            $(this).text("More");
            $(this).closest('div.ellipsis-text').css("max-height", "50px").dotdotdot({ after: "a", callback: dotdotdotCallback });
        }
    });

    function dotdotdotCallback(isTruncated, originalContent) {
        if (!isTruncated) {
         $("a", this).remove();   
        }
    }
});

It seems that the click event handler for the <div>'s anchor tags is getting removed, I am never able to reach the event handler after the More button is clicked.

Solution found:

Updated code:

$(function() {
    $("div.ellipsis-text").dotdotdot({
        after: 'a.more',
        callback: dotdotdotCallback
    });
    $("div.ellipsis-text").on('click','a',function() {
        if ($(this).text() == "More") {
            var div = $(this).closest('div.ellipsis-text');
            div.trigger('destroy').find('a.more').hide();
            div.css('max-height', '');
            $("a.less", div).show();
        }
        else {
            $(this).hide();
            $(this).closest('div.ellipsis-text').css("max-height", "50px").dotdotdot({ after: "a.more", callback: dotdotdotCallback });
        }
    });

    function dotdotdotCallback(isTruncated, originalContent) {
        if (!isTruncated) {
         $("a", this).remove();   
        }
    }
});

Thanks, guys!

Ehsan Sajjad

Change this:

$("div.ellipsis-text").find("a").click(function() {

to this:

$("div.ellipsis-text").on('click','a',function() {

UPDATED CODE:

$(function () {
    $("div.ellipsis-text").dotdotdot({
        after: 'a.more',
        callback: dotdotdotCallback
    });
    $("div.ellipsis-text").on('click', 'a', function () {

        if ($(this).text() == "More") {
            var div = $(this).closest('div.ellipsis-text');
            div.trigger('destroy').find('a.more').hide();
            div.css('max-height', '');
            $("a.less", div).show();
        } else {

            $(this).closest('div.ellipsis-text').css("max-height", "50px").dotdotdot({
                after: "a",
                callback: dotdotdotCallback
            });
        }
    });

    function dotdotdotCallback(isTruncated, originalContent) {
        if (!isTruncated) {
            $("a", this).remove();
        }
    }
});

the second way is to use single a tag and just toggle its text this way:

HTML:

    <div class='ellipsis-text'>"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?" 

<a class='more' href='#'>More</a>

 </div>

JQUERY:

$(function () {
    $("div.ellipsis-text").dotdotdot({
        after: 'a.more',
        callback: dotdotdotCallback
    });
    $("div.ellipsis-text").on('click', 'a', function () {

        if ($(this).text() == "More") {
            var div = $(this).closest('div.ellipsis-text');
            div.trigger('destroy')
            div.css('max-height', '');
            $(this).text("Less");

        } else {
            $(this).text("More")
            $(this).closest('div.ellipsis-text').css("max-height", "50px").dotdotdot({
                after: "a",
                callback: dotdotdotCallback
            });
        }
    });

    function dotdotdotCallback(isTruncated, originalContent) {
        if (!isTruncated) {
            $("a", this).remove();
        }
    }
});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事