How to append html like plain text to page

Viewed 212

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 Answers

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.

Related