I want to Toggle between HTML code and Preview html using textarea
When HTML is clicked it would show the code hiding the textarea
and when again HTML is clicked it would show back again
I want to have design Something like this
I have tried with
<style>
.container {
width:500px;
position: fixed;
}
.right-element {
background: red;
display: inline-block;
position: absolute;
right: 0;
}
</style>
<div class="container">
<div class="right-element">
Preview
</div>
<textarea id="w3review" name="w3review" style="position:relative;width:100%;height:75%;resize: none; " ><h3>At w3schools.com you will learn how to make a website. They offer free tutorials in all web development technologies.</h3></textarea>
</div>
4
Answers
You could have another
div
specifically for showing the preview. Then, when the user toggles the HTML view, insert the textarea’s value into theinnerHTML
of thediv
and show it.This could expose your application to XSS attacks though, so be careful when using it.
Try this
Textarea can’t display html, you can use div with attribute contenteditable instead like this:
Hope this help!
This solution has a button that toggles between "Preview" and "Edit" using jQuery click. On click it grabs the textarea value, and sets a div with the value as HTML.