There could be a situation where we might be store both HTML decoded text as well as HTML text such as email message.

Generally, we use to have some text editors to the user on the screen to provide the template text with some styles. Now, the real challenge is to decode this HTML text as all text editors don’t provide HTML decoded text. To get HTML decoded text following is the simple JavaScript method which will fulfill our requirement.

Let’s take following is the email template provided in the text editor by the user and the same is providing by text editors by its value property. 

<p>Hi --First Name=there--,</p> 
<h1><strong>Congratulations on signing up<br> 
to TechXposer!</strong></h1> 

<h3>Thanks for joining our free community for programmers. <br/>
We're excited to share everything that we know about new technologies its challenges.</h3>

Now, we also need the above string as decoded as follows 

Hi --First Name=there--,
Congratulations on signing up
to TechXposer!

Thanks for joining our free community for programmers.
We're excited to share everything that we know about new technologies its challenges.

Also, here we need to maintain the new lines. Following is the simple couple of JavaScript methods with the help of jQuery we can achieve it. 

function htmlDecodeByPreserveLine(htmlString) { 

    var htmlLines = htmlString.split('<br>' | '<br/>'); 

     for (var i = 0; i < lines.length; i++) { 

        htmlLines[i] = htmlStringDecode(lines[i]); 
    } 

    return lines.join('<br/>'); 
} 

function htmlStringDecode(htmlString) { 

    return $('<div/>').html(htmlString).text(); 
}

Happy Coding 🙂