P Griep
P Griep

Reputation: 844

Convert string to html tags in Javascript

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?

Upvotes: -1

Views: 7914

Answers (2)

Abozanona
Abozanona

Reputation: 2295

Try createElement

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

Upvotes: 0

mohamedrias
mohamedrias

Reputation: 18576

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

Upvotes: 4

Related Questions