2

If I have a textarea including some HTML code, how can I write a JavaScript function to show the HTML output instead of HTML code itself, for example:

<textarea id="mytextarea">
<table border=1><tr><td>cellone</td>td>celltwo</td></tr></table
</textarea>
<input type=button onclick="HTMLoutput();"/>

<script>
HTMLoutput()
{
//Code to show html output instead of html code in textarea
}
</script>

How can I do this? What is the suggested code to write inside the HTMLoutput()?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
user2888402
  • 77
  • 1
  • 1
  • 9
  • possible duplicate of [How can I show HTML formatting inside a textarea?](http://stackoverflow.com/questions/7785607/how-can-i-show-html-formatting-inside-a-textarea) – DevlshOne Oct 18 '13 at 03:23

2 Answers2

1

It sounds like you're asking how to take some HTML and display its rendered result in your document.

That's exactly what the innerHTML property does.
Simply pick a DOM element to display the result in, and set its innerHTML to the HTML to display.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

So to convert the the html code to a formated html you need to do:

$('resultDiv').append($('<div/>').html($('.txtArea').val()+"<br>");

here's an example that use div with contentEditable set to true.

Claudio Santos
  • 1,307
  • 13
  • 22