0

I would like to create a "read more" button that only will show if the text inside a wrapper is breaking.

This is my code so far:

HTML

<div class="comment-wrapper">
    <div class="text">
        Lorem ipsum. Lorem ipsum Lorem ipsum. Lorem ipsum Lorem ipsum. ipsum. Lorem ipsum
        Lorem ipsum. Lorem ipsum Lorem ipsum. Lorem ipsum Lorem ipsum. Lorem ipsum
        Lorem ipsum. Lorem ipsum.
    </div>
    <div class="read-more">Read more</div>
    <div class="read-less">Read less</div>
</div>

CSS

.read-more{
    font-size: 12px;
    margin-top: 12px;
    cursor: pointer;
    display: block;
    text-align: left;
}
.read-less{
    font-size: 12px;
    margin-top: 12px;
    cursor: pointer;
    display: none;
    text-align: left;
}

.comment-wrapper.open .read-more {
    display: none;
}
.comment-wrapper.open .read-less {
    display: block;
}

.comment-wrapper .text {
    text-align: left;
    font-size: 12px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}
.comment-wrapper.open .text {
    display: block;
}

JS

jQuery(".read-more").on("click",function(){
    jQuery($(this)).parent('div').addClass('open');
});

jQuery(".read-less").on("click",function(){
    jQuery($(this)).parent('div').removeClass('open');
});

How can I achieve this within this code?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
pos-sudo
  • 21
  • 4
  • Does this answer your question? https://stackoverflow.com/questions/143815/determine-if-an-html-elements-content-overflows – isherwood Apr 15 '21 at 15:50

2 Answers2

1
window.addEventListener('load', function() {
  $('.comment-wrapper').each(function() {
    var scrollHeight = this.getElementsByClassName('text')[0].scrollHeight;
    var clientHeight = this.getElementsByClassName('text')[0].clientHeight;
    var thisElem = this;
    if ( scrollHeight > clientHeight ) {
      $(this).children('.read-more').show();
    } else {
      $(thisElem).find('.read-more, .read-less').hide();
    }
    $(this).children(".read-more").on("click", function() {
      console.log('done');
      $(thisElem).find('.read-more, .read-less').toggle();
    });

    $(this).children(".read-less").on("click", function() {
      $(thisElem).find('.read-more, .read-less').toggle();
    });
  });
});
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
garex aka
  • 11
  • 2
0
window.addEventListener('load', function() {
     var scrollHeight = document.getElementsByClassName('text')[0].scrollHeight;
     var clientHeight = document.getElementsByClassName('text')[0].clientHeight;

     if( scrollHeight > clientHeight ) {
        $('.read-more').show();
     } else {
        $('.read-more, .read-less').hide();
     }
     $(".read-more").on("click",function(){
        $('.read-more, .read-less').toggle();
    });

    $(".read-less").on("click",function(){
        $('.read-more, .read-less').toggle();
    });
});
isherwood
  • 58,414
  • 16
  • 114
  • 157
garex aka
  • 11
  • 2