LilyJones
LilyJones

Reputation: 23

How to convert HTML to text without tags and special characters?

I have a series of quotes that look like this:

<p>Being a graphic designer gets you used to rejection of your brilliance. So it&#8217;s good practice for dating.  </p>

I would like them to look like this:

 Being a graphic designer gets you used to rejection of your brilliance. So it's good practice for dating.

I tried using innerHTML, with limited success.

Upvotes: 1

Views: 298

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115232

Create a temporary span element with the html content as string using document.createElement method. Later get the text content by getting the textContent property.

var str = '<p>Being a graphic designer gets you used to rejection of your brilliance. So it&#8217;s good practice for dating.  </p>';

// create a span element
var temp = document.createElement('span');
// set the html to the element
temp.innerHTML = str;
// get the text content
console.log(temp.textContent);

Upvotes: 3

Related Questions