2

I have html content with special characters

<div class="agfMailBodyHtml"><font size=2 face="sans-serif">Test mail 5 - reply op rich text with <b>attachements\r\n</b>also with <b>attchements</b></font>\r\n<br>\r\n<br><font size=2 face="sans-serif"><br>\r\n</font>\r\n<br>\r\n<br>\r\n<br>\r\n<br><font size=1 color=#5f5f5f face="sans-serif">

is it possible to convert it into html using javascript?

3
  • Do you mean add it to the document? Commented Apr 2, 2014 at 22:54
  • yes, the data i am getting from mid-tier is in the above format. i need to convert it to html Commented Apr 2, 2014 at 22:56
  • css-tricks.com/snippets/javascript/unescape-html-in-js Commented Apr 2, 2014 at 22:57

2 Answers 2

3

Yeah, it's possible:

var str = "<div class="agfMailBodyHtml"><font size=2 face="sans-serif">Test mail 5 - reply op rich text with <b>attachements\r\n</b>also with <b>attchements</b></font>\r\n<br>\r\n<br><font size=2 face="sans-serif"><br>\r\n</font>\r\n<br>\r\n<br>\r\n<br>\r\n<br><font size=1 color=#5f5f5f face="sans-serif">";

var div = document.createElement("div");
div.innerHTML = str;

div.innerHTML = div.textContent;
alert(div.innerHTML);
document.body.appendChild(div);

DEMO FIDDLE

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

1 Comment

Can you please explain your code? Why are we setting the innerHTML twice with the same value?
2
var el = document.createElement('div');
el.innerHTML = "<div class="agfMailBodyHtml"><font size=2 face="sans-serif">Test mail 5 - reply op rich text with <b>attachements\r\n</b>also with <b>attchements</b></font>\r\n<br>\r\n<br><font size=2 face="sans-serif"><br>\r\n</font>\r\n<br>\r\n<br>\r\n<br>\r\n<br><font size=1 color=#5f5f5f face="sans-serif">";
var html = el.textContent;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.