I'm trying to create my own code snippet area where the author can write pure html inside a code block which will be converted to text automatically. This works as demonstrated here:
https://jsfiddle.net/mtdrdvaw/2/
However, the line break of elements doesn't persist once converting them to text. I would like the formatting to remain the same after the HTML to text conversion.
Is this possible and how could I achieve this?
This is my code snippet markup:
<code class="code-snippet">
<div class="item">test</div>
<div class="item">test1</div>
<div class="item">test2</div>
</code>
And here's the script:
var codeSnippets = document.getElementsByClassName('code-snippet'),
length = codeSnippets.length,
i;
for (i = 0; i < length; i++) {
var code = codeSnippets[i].innerHTML,
node = document.createTextNode(code);
codeSnippets[i].innerHTML = '';
codeSnippets[i].appendChild(node);
}
EDIT: Since I was unclear, I've looked at samp, pre and code but none of them did what I wanted them to do. I want the area to use my own padding and look the way it is written in the code editor of the author. Using just pre for example gives it a ridiculous white space that I do not want.