0

Possible Duplicate:
HTML Entity Decode

I want to convert this string

<p>update this post</p> 

to

<p>update this post</p>

in Javascript.

Do you have any idea? Thanks in advance.

1
  • @Paul That one is focused on a specific entity, in addition to using jQuery. It was also asked later than the first one. Commented Jan 6, 2013 at 9:39

3 Answers 3

4

I'd create an element, set its innerHTML and get its innerText:

var element = document.createElement('div');
element.innerHTML = '&lt;p&gt;update this post&lt;/p&gt; ';
console.log(element.innerText);

The result is:

"<p>update this post</p> "
Sign up to request clarification or add additional context in comments.

2 Comments

This works fine for hardcoded values, but beware of using this for user input.
and, if you used jquery it would be as easy as this: $('<div />').html('&lt;p&gt;update this post&lt;/p&gt; ').text();
0

Working example:

var text='&lt;p&gt;update this post&lt;/p&gt;';
var d = document.createElement("div");
d.innerHTML=text;
alert(d.innerText || d.text || d.textContent);

Comments

-4

You can use this method:

var param = '&lt;p&gt;update this post&lt;/p&gt'; 
unescape(param);

1 Comment

The unescapefunction (which is deprecated because it can't handle Unicode properly) doesn't work on that form of escaping.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.