jQuery(function($) {
$(document).ready(function() {
var moreLink = 'Read more';
var lessLink = 'Read less';
// Save the original content
$('.container').each(function() {
var originalContent = $(this).html();
$(this).data('original-content', originalContent);
});
$('.container p').each(function() {
var container = $(this).parent();
var wordLimit = parseInt(container.closest(".container").attr('data-word-limit'));
var paragraphs = container.find('p');
var combinedText = '';
paragraphs.each(function() {
combinedText += $(this).text().trim() + ' '; // Combine paragraph texts
});
var words = combinedText.split(' ');
if (words.length > wordLimit) {
var truncatedText = words.slice(0, wordLimit).join(' ');
var remainingText = words.slice(wordLimit).join(' ');
var fullContent = '<p>' + truncatedText + '<span class="points">... </span>' + ' <span class="morecontent"><span>' + remainingText + ' </span><a href="" class="morelink">' + moreLink + '</a></span></p>';
$(this).parent().html(fullContent);
}
});
// Use event delegation to handle click events for dynamically added elements
$(document).on('click', '.morelink', function() {
var widget = $(this).closest('.container');
var originalContent = widget.data('original-content');
widget.html(originalContent);
widget.find('p:last').append('<a href="" class="lesslink">' + 'Weniger lesen' + '</a>');
return false;
});
$(document).on('click', '.lesslink', function() {
$(this).closest('p').each(function() {
var container = $(this).parent();
var wordLimit = parseInt(container.closest(".container").attr('data-word-limit'));
var paragraphs = container.find('p');
var combinedText = '';
paragraphs.each(function() {
combinedText += $(this).text().trim() + ' '; // Combine paragraph texts
});
var words = combinedText.split(' ');
if (words.length > wordLimit) {
var truncatedText = words.slice(0, wordLimit).join(' ');
var remainingText = words.slice(wordLimit).join(' ');
var fullContent = '<p>' + truncatedText + '<span class="points">... </span>' + ' <span class="morecontent"><span>' + remainingText + ' </span><a href="" class="morelink">' + moreLink + '</a></span></p>';
$(this).parent().html(fullContent);
}
});
return false;
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div class="container" data-word-limit="90">
<div class="paragrapgs">
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam <strong>nonumy eirmod tempor</strong> invidunt ut labore et dolore magna...</p>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam...</p>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam...</p>
</div>
</div>
This JS code truncates the Text depending on the "data-word-limit" of the Container, and creates a read more button to toggle the text.
So I need to keep the HTML structure for an example: <strong>
Tag. but when the Text is truncated it loses the structure.
Thank you
I have tried to split the text without losing the structure but it did not work
2
Answers
I Have found the Answer my self, in Jquery instead of .text() split I schould make it .html() split as fowllowing:
It may not be exactly what you’re looking for, but you can actually do something like this without any JavaScript whatsoever: