-1

I want to convert the following string to HTML tags and place it inside my div.

<strong>asdfadfsafsd</strong>

I am using the following code to place it inside my div:

var message = "<strong>testmessage</strong&gt";
document.getElementById('message').innerHTML = bericht;

The problem is that I see the following in my div now:

<strong>testmessage</strong>

But I want to see: testmessage

What is the problem?

2 Answers 2

4

var string = "&lt;strong&gt;asdfadfsafsd&lt;/strong&gt;",
    results = document.getElementById("results")
    results.innerHTML = string;
    results.innerHTML =    results.textContent;
<div id="results"></div>

At first load the it as html. Then fetch it as text and then again load it as HTML :)

Refer HTML Entities

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

3 Comments

and a link to HTML character entities : dev.w3.org/html5/html-author/charref
Easy and simple! Just what I needed!
You're welcome :) Also refer to that link by @Kaiido as well. It will be useful to you :)
0

Try createElement

var tag = document.createElement("strong");
var t = document.createTextNode("testmessage");
tag.appendChild(t);
document.body.appendChild(tag);

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.