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()?

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

i want to build an editor and to show the rendered result inside the textarea itself as a preview
@user2888402: <textarea> can only show plain text. You can use CSS to show a preview on top of the textbox.
thanks @Slaks can you please give me an example how to do this using CSS?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.