0

Is it possible to get the name of HTML-Special-Characters programmatically in JavaScript? Per example something like here:

function HtmlEncode(s)
{
  var el = document.createElement("div");
  el.innerText = el.textContent = s;
  s = el.innerHTML;
  return s;
}

Test run:
alert(HtmlEncode('&;\'><"'));

Output:
&amp;;'&gt;&lt;"

But the function of @Cerebrus (see link above) doesn't work always.
alert(HtmlEncode('$§©'));

Per example it fails for following characters:

$ : &dollar;
& : &amp;
§ : &sect;
© : &copy;
...

Any idea, how can I get the name of HTML Special Characters?
(I mean programmatically like above, but no list-name like a map).
Thanks in advance

user3815508
  • 369
  • 4
  • 20
  • I'm pretty sure you need a map, no matter if you compile it yourself or use [someone else's](https://github.com/mathiasbynens/he/blob/master/data/entities.json). – Álvaro González May 12 '18 at 08:11
  • Someone left a comment (now removed) with a good point about the use case. It'd be interesting to know why exactly you need this feature for (it isn't obvious at all)—that can help to get alternative solutions you might not have considered. – Álvaro González May 12 '18 at 12:05
  • 1
    I'm trying to create a javascript tool, which will be able to show all html special characters. It would be nice, if the tool would show the name of the characters too. A map will be to large! So I'm looking for a small solution, otherwise I'll give it up :). – user3815508 May 12 '18 at 12:38

0 Answers0