Question

So, I have this issue and I have been trying to fix it for the past few days with no luck. I'm using Jquery.dotdotdot

What is the issue?

When I click "Read More" it will expand the content, when I click "Read Less" it will hide the expanded content. If I click "Read More" for a SECOND time it will not work.

JSFiddle:

Live Website:

HTML:

<div class='article'>
<div class='articleheader'>Title</div>
<div id='article_$count' class='articlecontent'>
    <p>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 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? On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee
       <a href='#' class='readless'>Read Less</a>
    </p>
</div>
</div>

CSS: (Doesn't matter, just added to fiddle for improved visibility)

jQuery:

/*Adds the "Read More" link and truncates the text when clicking read Less. */
$('.readless').click(function () {
$(this).parents('div').eq(0).dotdotdot({
    ellipsis: '... ',
    wrap: 'word',
    fallbackToLetter: true,
    after: '<a href="#" class="readmore">Read more &raquo;</a>',
    watch: false,
    height: 100,
    tolerance: 10,
    lastCharacter: {
        remove: [' ', ',', ';', '.', '!', '?'],
        noEllipsis: []
    }
});
});

/*Truncates all articles */
$("[id^=article]").dotdotdot({
    ellipsis: '... ',
    wrap: 'word',
    fallbackToLetter: true,
    after: '<a href="#" class="readmore">Read more &raquo;</a>',
    watch: false,
    height: 100,
    tolerance: 10,
    lastCharacter: {
    remove: [' ', ',', ';', '.', '!', '?'],
    noEllipsis: []
}
});


/*removes truncation and shows the hidden "Read Less" link in content*/
$('.readmore').click(function () {
    $(this).parents('div').eq(0).trigger("destroy");
});
Was it helpful?

Solution

Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the event binding call.

As readmore anchor is created dynamically, You need to use Event Delegation. You have to use .on() using delegated-events approach.

Use

/*removes truncation and shows the hidden "Read Less" link in content*/
$(".article").on('click', '.readmore', function () {
    $(this).parents('div').eq(0).trigger("destroy");
});

DEMO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top