Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 440
HP. Avatar asked Feb 09 '10 05:02

HP.


1 Answers

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;
like image 57
Christian C. Salvadó Avatar answered Nov 10 '22 00:11

Christian C. Salvadó