I am looking for a way to display only a part of the Overview initially and after clicking Read more, the rest is displayed.
I have this implementation of Read more – Read less in Django:
HTML:
<p><strong>Overview:</strong>
<span class="overview">{{ game.overview|default:"N/A"|slice:"300"}}</span>
{% if game.overview|length > 300 %}
<span class="dots">...</span>
<span class="more" style="display: none;">{{ game.overview|default:"N/A"|slice:"300:" }}</span>
<button class="read-button" onclick="readMore(this)">[Read more...]</button>
{% endif %}
</p>
JavaScript:
function readMore(button) {
var parent = button.parentNode;
var dots = parent.querySelector(".dots");
var moreText = parent.querySelector(".more");
if (dots.style.display === "none") {
dots.style.display = "inline";
button.innerHTML = "[Read more...]";
moreText.style.display = "none";
} else {
dots.style.display = "none";
button.innerHTML = "[Read less...]";
moreText.style.display = "inline";
}
}
My problem is that when the text get’s sliced inside a word like in this example:
Created in Unity, and designed to be played in-browser, players make a
series of moral and dialogue choices within each chapter, before being
presented a larger question at the end of eac … [Read more…]
When I am clicking the [Read more…], the output is like this and the word ‘each’ get’s separated where the slice occurred.:
Created in Unity, and designed to be played in-browser, players make a
series of moral and dialogue choices within each chapter, before being
presented a larger question at the end of eac h chapter, asking what
should happen next in the story.
What should I do so that when the slicing happens inside a word it is not separated when clicking the button.
2
Answers
I have managed to solve my problem! Just in case someone has a similar issue, this is how I managed to do it:
HTML:
JS:
You are just hiding and showing text with js without identiying if the word which will show on Read more button have some space between last character and newly shown paragraph.
I think you need to change your js code to slice at the last space character.
With this it will look for last word, if the last word have some space between newly text after button click.
And the problem was in
js
it got nothing to do with django.