2

I'm Working on dynamically code the data coming from DB, and i need to all Li Element by append in JavaScript no jQuery:

This is my Code:

const x = document.getElementById('messagesContent')
x.append(`<li class="message"><b>User</b><br/>${message}</li>`);

The Result is:

<li class="message"><b>User</b><br/>xxxxxxxx</li>

Come as a text not html

Ramy Mohamed
  • 168
  • 2
  • 13

1 Answers1

0

You need appendChild function and pass a node element instead of a string.

You must create a list element via createElement and assign proper attributes and the innerHTML which is intended.

const x = document.getElementById('list');
const li = document.createElement('li');
li.class = 'message'
li.innerHTML = `<b>User</b><br/>second`;
x.appendChild(li);
<ul id="list">
  <li>first</li>
</ul>
Eugen Sunic
  • 12,218
  • 8
  • 60
  • 78