Skip to content

JavaScript encode HTML entities | Example code

  • by

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:

JavaScript encode HTML entities

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

Leave a Reply

Your email address will not be published. Required fields are marked *