JavaScript encode HTML entities | Example code

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 doubt and 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.