6

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.

2 Answers 2

17

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;
Sign up to request clarification or add additional context in comments.

1 Comment

See what jQuery does to developers, they now expect every API to be a function :)
0

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.

1 Comment

That doesn't append, bmdiv.innerHTML += str; is

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.