1

I have the following HTML:

<p>This contains an HTML space entity &#160;.</p>

I need to serialize this HTML to text along with HTML entities as their existing code (spaces added to prevent SO from rendering literal characters):

< p >This contains an HTML space entity & #160;.< / p >

When serializing the HTML the HTML entities are rendered instead of converted to their code/text form:

new XMLSerializer().serializeToString(element)

I've looked in to other methods of converting HTML code to text including innerHTML though I haven't managed to determine any direct means to outputting the HTML code that exists without it being modified by the browser.

I'm also open to replacing HTML entities with a createTreeWalker if need be though I'd prefer a more direct approach. No frameworks. Suggestions please?

John
  • 1
  • 13
  • 98
  • 177
  • I would try grabbing the innerHTML, and escaping the   manually (maybe with a little function that you can find around online), then when it gets outputted, it will unescape naturally and put back what you had there. – spozun Sep 19 '16 at 20:12

1 Answers1

0

Please see this SO answer: https://stackoverflow.com/a/3700369/3218479.

You can use code like:

// Prepare element
var myEl = document.createElement("p");
myEl.innerText = "This contains an HTML space entity &#160;.";

// Convert to string
var textArea = document.createElement("textarea");
textArea.innerHTML = myEl.outerHTML;
var myElText = textArea.innerText;
delete textArea;
Community
  • 1
  • 1
cema-sp
  • 362
  • 2
  • 8