3
I would like to make a "read more" and "read less" button with Pure Javascript (no jQuery). However, this text comes dynamically from the database and I use a PHP foreach to fill the Divs with these texts coming from the database.
These texts can have up to 3000 characters, however, I would like to only show 1000, and if the user clicked on "read more" then the 3000 characters would be shown. So if soon after I clicked on "read less", then the text of the div would return to 1000 characters.
I could see some examples, but the text was not dynamically filled and a "span" tag was used embracing the text that would be removed or shown.
Only in my case it is not possible to put this tag "span" precisely because the text is dynamic and comes from the database.
Follows my code:
<?php foreach ($feed['posts'] as $data) { ?>
<div class="feed-item-body mt-10 m-width-20 post-body">
<?php echo nl2br($data->body['body']); ?>
</div>
<?php } ?>
My content (text) is in that $data->post['body'].
Below is my code in a more verifiable way. Let’s imagine that the text inside the div came from the bank
<div class="feed-item-body mt-10 m-width-20 post-body">
(IMAGINEM QUE ESSE SEJA UM TEXTO COM 3000 CARACTERES) Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</div>
There’s this example here from w3Schools: Example:
But note that it is a static text and they use a span. If in my case it was a static text, it would be much easier.
So how would a Javascript code look to show and collect the text according to its number of characters ?
Related: https://answall.com/a/169912/4995
– Wallace Maxters
Related: https://answall.com/questions/117961
– Wallace Maxters
I’ll take a look. Thank you. I did a lot of research and was finding nothing that came close to my toil. I’ll see.
– Gato de Schrödinger
You asked the other question if the "text dies". Yes, it dies. The idea is to have two
div
. A with the full text you leave hidden, and a with cut text you leave displaying. When you click on the link, you display the full text and hide the other.– Wallace Maxters
"I would like to make a "read more" and "read less" button with Pure Javascript (no jQuery). However, this text comes dynamically from the database and I use a PHP foreach to fill the Divs with these texts coming from the database" I can’t see where this changes anything, after PHP has run the result is a static HTML. JS only runs after.
– Bacco
Even, I included the [php] tag, since most solutions can be found in php itself, which is used in the question.
– Wallace Maxters
@Bacco, then. I had the view that changes, because when they are static texts, we can fit tags as span within these texts. So we can hide these texts that are inside these tags easily, and then show them through a JS event. However, with the dynamic text coming from the bank via PHP, this text is already mounted and there would be no way to put tags in the middle of it to hide part of the text. I don’t know if I can express myself well...
– Gato de Schrödinger
@Wallacemaxters, but this idea of yours was a great idea. I will try to apply it here and return with the result. I think you could even work out a response based on what you said. You don’t have to code. Explaining in context I already accept it. Thank you!
– Gato de Schrödinger
@Gatodeschrödinger understand I understood, I just don’t know how to make it fit on the site, the question doesn’t have a [mcve], other alternatives have already been posted with CSS on the site, the way it is was a "do it for me", even if it’s not the intention. A [mcve] would be fundamental to keep the post open (having as someone copy your PHP with a real text (same as the database) mounting a div.
– Bacco
With CSS I believe it already has solution on the site, including (limiting space with CSS div and overflow:Hidden) - even for modern browsers or need JS https://answall.com/a/110958/70
– Bacco
@Bacco , I will delete the post. Good
– Gato de Schrödinger
Did you see the CSS example? There are many ways to adapt, you can change the class with JS if you want to avoid checkbox. Better than erasing would be [Edit] and for the minimum verifiable example, then it would be useful for a broad audience
– Bacco
My intention was not a do for me. It’s because I didn’t even know. And I really wanted in JS Vanilla. That’s why I made it clear in the title of the question.
– Gato de Schrödinger
So. The Minimal and Verifiable Example helps everyone. Until you get on top of it we can come up with an official answer that solves what you need and serves those who search it later. That’s the idea of the site. And it’s something you do in a minimal amount of time,
– Bacco
@Bacco , but the minimum and verifiable example is just the one I have. Only the string is displayed via PHP and as it comes from the database. Other than that, I wouldn’t have any more useful code.
– Gato de Schrödinger
I think it’s worth a read on [mcve]. Verifiable, minimal and complete is if I take the code, save it as PHP here (or open it in an ideone) and exit the HTML the way you have today. That is for us to have the same result as you, but without creating a bank for it (ex, with a variable in place). And "minimum" means taking away everything that doesn’t change the problem. If you master the concept, you make your time on the site more income for you and everyone involved.
– Bacco