3

I have <li> in DOM, want to insert string with some HTML like plain text. But it appends with html. Sorry for bad English.

Example

<li></li> - empty list element

I want to append string <script>alert(1)</script> to li like text, without execute.

1 Answers1

4

You can use innerText, like so:

document.querySelector('li').innerText += '<script>alert(1)<\/script>';
<li></li>

However, remember that you will have to escape certain characters.

Angelos Chalaris
  • 6,611
  • 8
  • 49
  • 75