Easy way to use textArea to encode HTML entities.
=> ®
& => &
© => ©
™ => ™
JavaScript encode HTML entities Example code
HTML example code.
<!DOCTYPE html>
<html>
<body>
<script>
function encodeHTMLEntities(text) {
var textArea = document.createElement('textarea');
textArea.innerText = text;
return textArea.innerHTML;
}
console.log(encodeHTMLEntities("<img src='myimage.jpg'>"));
</script>
</body>
</html>
Output:
Do comment if you have any doubts or suggestions on this HTML example code.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version