3

I am stuck trying to bold a single word after i change the text content in HTML via JavaScript.

Here is my code:

document.getElementById('par3').textContent = "I <b>love</b> JS";
<!DOCTYPE html> <html> <head> <title>Homework05</title> </head> <body> <p id="par1">Unchanged Paragraph</p> <p id="par2">Unchanged Paragraph</p> <p id="par3">Unchanged Paragraph</p> <p id="par4">Unchanged Paragraph</p> <p id="par5">Unchanged Paragraph</p> <script src="script.js"></script> </body> </html>

Somehow, the tags are not rendered. Is there any specific escape characters for this? Or this is more complex thing to accomplish?

2 Answers 2

2

You need to use innerHTML. textContent will escape your string.

document.getElementById('par3').innerHTML= "I <b>love</b> JS";
<!DOCTYPE html> <html> <head> <title>Homework05</title> </head> <body> <p id="par1">Unchanged Paragraph</p> <p id="par2">Unchanged Paragraph</p> <p id="par3">Unchanged Paragraph</p> <p id="par4">Unchanged Paragraph</p> <p id="par5">Unchanged Paragraph</p> <script src="script.js"></script> </body> </html>

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

1 Comment

Tnx a lot, u saved me :)
1
use innerHTML other then textContent or 

choose the word and define it to string var then use string.bold() method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.