Skip to main content

Posts

Showing posts with the label JavaScript

Converting HTML special characters to its equivalent HTML Entity

Here we will see how to convert the HTML text to its equivalent entity. Sometime we come across a situation where we wish to show the content of inner HTML of some specified element for viewing or debugging purpose. I tried to write a code for the conversion. Here is code and its usage: function convertToHTMLEntity(html){ var textNode = document.createTextNode(html); var divElement = document.createElement('div'); divElement.appendChild(textNode); return divElement.innerHTML; } console.log(convertToHTMLEntity('<p> Hello! </p>')); Output: See the Pen Converting HTML special characters to its equivalent HTML Entity by MI ANSARI ( @miansari ) on CodePen .