0

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:

 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.

4
  • 2
    Well your code explicitly creates the extra <div> that you don't want; perhaps you should instead not do that. Commented Nov 6, 2021 at 19:05
  • you wrap the post in a div ? Commented Nov 6, 2021 at 19:05
  • Is this your full code? I think you're using a loop and appending the item every time Commented Nov 6, 2021 at 19:07
  • 1
    I did so because I DIDNT KNOW another way, but the guy below already gave me the answer and now I know. Commented Nov 6, 2021 at 19:29

1 Answer 1

2

You can use insertAdjacentHTML:

messages.insertAdjacentHTML('beforeend', post)
Sign up to request clarification or add additional context in comments.

Comments

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.