HP.
HP.

Reputation: 19896

Add html string to DOM as element

Is there anyway to create a string and add to the DOM? And having Javascript to understand the elements in the string?

I tried the below and 4th line gives error:
var bmdiv = document.createElement('div');
bmdiv.setAttribute('id', 'myDiv');
var str = "<b>aa</b>";
bmdiv.innerHTML(str);

I need to add several tags in str to the DIV myDiv

I need NOT to use jQuery since the script will not load jQuery Thanks.

Upvotes: 6

Views: 17581

Answers (2)

Waleed Al-Balooshi
Waleed Al-Balooshi

Reputation: 6406

Try

bmdiv.innerHTML = str;

Another way to do this is to manually create the DOM structure for each of the tags, then append them into the div.

Upvotes: 0

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 828100

The innerHTML property is not a function, you should assign it like this:

var bmdiv = document.createElement('div');
bmdiv.setAttribute('id', 'myDiv');
var str = "<b>aa</b>";
bmdiv.innerHTML = str;

Upvotes: 17

Related Questions