Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to append an html element from a string?

Hi,

I have a code like this, first my fixed html which is the container:

 <div id="messages"></div>

then the javascript that generates its contents:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 var post = `<div id="${userid}">${content}</div>`;
 var item = document.createElement('div');
 item.innerHTML = post;
 messages.appendChild(item);

the problem with this is that it wrapps each post in a div which I dont need. This should be the output

<div id="messages">
 <div id="user45">some content</div>
 <div id="user46">more content</div>
</div>

how can I append the content of the string directly to the container without the wrapper?

Thank you.

>Solution :

You can use insertAdjacentHTML:

messages.insertAdjacentHTML('beforeend', post)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading