hi all guys
i need your help with this code, actually this code works fine and trim all text to specified lenght, but actually if there are images into post, this are completely removed. so i need to limit image to 0, and if someone click on “read more” can see all the images…here my code so far:
$(document).ready(function () {
// Limite caratteri settabile da pannello amministrazione
const max = app.forum.attribute('Lenght');
$('.Post-body').each(function () {
var str = $(this).text();
if ($.trim(str).length > max) {
var subStr = str.substring(0, max);
var hiddenStr = str.substring(max, $.trim(str).length);
var ReadMore = app.translator.trans('flarum-ext-readmore.forum.readmore');
$(this).empty().html(subStr);
$(this).append(' <a href="javascript:void(0);" class="linkReadMore">' + ReadMore + '</a>');
$(this).append('<span class="addText">' + hiddenStr + '</span>');
}
});
$('.linkReadMore').click(function () {
$(this).siblings('.addText').contents().unwrap();
$(this).remove();
That is a misspelling that may be causing problems.
Also, do you have the complete code? If it isn’t available in an online, is there a repo? I’m not completely sure what you are saying, but I think if I see the code it may click.
I’m sorry, I’m not able to understand what you want and I don’t know how to get your repo to work so I can’t figure out how to help you. Maybe someone smarter than me will have an idea.
this extension simply add a readmore button after length value (the famous typo )
but acually if inside text there are images? these are completely removed.
i want to include image into “read more”.
I see you are using text, but as the name implies that will only get back text content, skipping all other elements (like images).
There’s a method for jQuery that gives you back all the content, not just text. (don’t remember the name since it’s been forever since I used jquery), I think it’s content() but I let you search for it.
With that, you can then filter the content as you see fit.
For example a possible approach would be to store all the images and then “reconstruct” them later.
But honestly this feels like a lot of work.
Unless you have a specific reason to remove text, in general the common approach is to hide content with CSS and show it again on click; opposed to actually remove it and re-add it.
With a more structured page you can fine tune to your need.
But pretty much what I am doing is setting a max height with JS on load equal the whole content.
Then by default it shows only a portion.
On hover uses the full height.