0

The link below is supposed to reveal more text when clicked, which it does, however the text uses all default styling, which cannot be seen on my black background that I use for the page. I have tried to style the tag where the text is added through JavaScript, but the added text still uses the default style.

Below is the code I have used: HTML:

<p>


    This movie might seem boring to you at first and you 
      might not get it after <br>first time viewing, like I did. But this 
    is one of the greatest movies ever made.....
        <a  href="#" onClick="more(event)">Read More</a>
    <p id="atext" class="atext">dqdas</p>
        <script src="Home.js"></script>
    </p>

and JavaScript:

function more(e) {
    e.preventDefault();
document.getElementById("atext").style.color= "white";
document.getElementById("atext").outerHTML = 'It touches many issues in our lives and packs a hell of a plot twist';

}
5
  • 1
    Unless necessary, it’s advisable to use innerText instead of outerHTML Commented Oct 11, 2019 at 22:18
  • @AlexH This solves my issue, as now all of my style is reflected in the added text. Thank you very much! Commented Oct 11, 2019 at 22:25
  • Great to hear. Please accept my answer to signify to the community that this problem has a solution. This allows others with the same problem to more easily find a fix. @Dante Commented Oct 11, 2019 at 22:26
  • @AlexH you made a comment, I cannot accept a comment on this site. It must be a question. If you make the answer with the same commend, I will label it as a solution. Commented Oct 11, 2019 at 22:49
  • I did make an answer @Dante Commented Oct 11, 2019 at 22:50

1 Answer 1

1

Simply use innerText rather then outerHTML. This should solve the issue.

I believe this happens because setting the HTML of an element overwrites any styles it may have. Setting the text does not do this.

Also, it is generally best to use innerText when necessary, as it reduces the risk of errors like yours occurring.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.