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:
Comments
Post a Comment