I'm trying to toggle a paragraph preview/full text and cannot alter the HTML, other than insert a <script> // brute force js </script>
block. The element with the paragraph injected dynamically and I have a function which does that successfully, but my problem is toggling my isPreview
boolean such that the elm.innerHTML
ternary operator line gets the updated value.
I've tried creating an external togglePrview(){}
function but that created other problems.
Thoughts on how to achieve this as elegantly as possible?
waitForelm(){...}
let isPreview = true;
waitForElm(".div_with_html").then((elm) => {
let linkText = isPreview ? "Show more" : "Show less";
let link = `. . . <a onclick="isPreview = !isPreview">${linkText}</a>`;
let fullBio = elm.innerHTML;
let preview = fullBio.substr(0, 450) + link;
elm.innerHTML = isPreview ? preview : fullBio; // does not get the updated value
});