I have a place where I am trying to show code in a text area. I have dumbed down the example but basically user can input some fields, click a button, and the code snippet displays in a text area below for them to edit if they want and then copy.
I finally got it working to display the actual code snippet. But now I can’t seem to find a way to format it so that it is indented and looks nice.
var mySnippet =
"<div id=\"myOuterDiv\">"
+ "<div id=\"myInnerDiv\">"
+ "</div>"
+ "</div>";
$('#mySnippetArea').text(mySnippet);
Which displays in my text area like so.
<div id="myOuterDiv"><div id="myInnerDiv"></div></div>
Where as I would like to see it as:
<div id="myOuterDiv">
<div id="myInnerDiv">
</div>
</div>
I’d really like to avoid any 3rd party plugins as its for work an a pain to get approval. It’s just 4 small snippets I need to format.
UPDATE
This appears to work as I need.
var mySnippet =
"<div id=\"myOuterDiv\">\r"
+ " <div id=\"myInnerDiv\">\r"
+ " </div>\r"
+ "</div>\r";
Output:
<div id="myOuterDiv">
<div id="myInnerDiv">
</div>
</div>
Add a
\nif you want a new line likeSingle character escape sequences:
Example: http://jsfiddle.net/jtx7e/