Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display read more button

I want to use a read more button after a text is larger than 300 characters.

I use this jQuery to fix this, but it is not working as I want.

var $j = jQuery.noConflict();
$j('.reviewtekst').each(function() {
    var $pTag = $j(this).find('p');
    if($pTag.text().length > 300){
        var shortText = $pTag.text();
        shortText = shortText.substring(0, 300);
        $pTag.addClass('fullArticle').hide();
        $pTag.append('<a class="read-less-link">Lees minder</a>');
        $j(this).append('<p class="preview">'+shortText+'</p><div class="curtain-shadow"></div><a class="read-more-link">Read more</a>');
    }
});

$j(document).on('click', '.read-more-link', function () {
    $j(this).parent().hide().prev().show();
});

$j(document).on('click', '.read-less-link', function () {
    $j(this).parent().hide().next().show();
});

See this JSFiddle: https://jsfiddle.net/8cm67cun/1/

How can I make this work, to display the <a> class outside the <p> class.

like image 357
JGeer Avatar asked Sep 28 '22 15:09

JGeer


1 Answers

Here is updated version https://jsfiddle.net/8cm67cun/2/ now it works fine with a tag outside the p

$j(document).on('click', '.read-more-link', function () {
    $j(this).hide().parent().find('.preview').hide().prev().show();
});

$j(document).on('click', '.read-less-link', function () {
    $j(this).parent().hide().next().show();
    $j(this).parents('.reviewtekst').find('.read-more-link').show();
});
like image 145
Aleksandar Gajic Avatar answered Oct 05 '22 05:10

Aleksandar Gajic