Render HTML inside a textarea

Asked by: chris11
Viewed: 317
Answers: 1
  • 0

I was wondering if it’s possible to render HTML inside a textarea? So if I place HTML content in it, it would be instantly rendered and displayed.


Answer by: ChristianKovats

Answered on: 01 Feb 2023

  • 0

You can’t really render HTML in a textarea. What you can do is to add a separate div with the contenteditable attribute set to true. And display the textarea content into that div.

You would want to do something like this:

<textarea id="text" cols="30" rows="10">
Hello this is <strong>just</strong> a <em>text</em>.

<div id="preview" contenteditable="true"></div>

and then inject the content of the textarea into the div with javascript

const textArea = document.getElementById('text');
const preview = document.getElementById('preview');

preview.innerHTML = textArea.textContent;


Note that this is not instant. For that you would probably need some more Javascript magic.

Please log in to post an answer!