1

I'm struggling with the simple syntax of outputting HTML entities.

For the sake of simplicity:

document.write("é");

I want to output é as é.

I've tried it with quotation marks, apostrophes, and no marks.

The only way I've managed to get it to work is /é/ but that outputs /é/.

Can somebody inform me of the correct way of doing this (and if possible explain why the slashes are doing their thing)?

I'm not familiar with jQuery so I'd appreciate if you keep answers in core JavaScript.

https://jsfiddle.net/e2ezz2h1/

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Rilke
  • 85
  • 4
  • 2
    Don't use `document.write`, use `innerHTML` (if you want to use entities) or `textContent` (if you want to keep the ampersands) – Bergi Sep 14 '16 at 14:20
  • I'm actually trying to write it into a textbox's `value` rather than its `innerHTML`. https://jsfiddle.net/esqrg8Lc/ – Rilke Sep 14 '16 at 14:30
  • 1
    Non-jQuery answer here: http://stackoverflow.com/a/1395954/74757 – Cᴏʀʏ Sep 14 '16 at 14:42
  • @Rilke for that, you should just use `"é"`. Where are you getting the entity from? – Bergi Sep 14 '16 at 15:31

2 Answers2

1

Based on this answer: https://stackoverflow.com/a/9609450/2637098 and the additional information you provided, this code should suit your needs.

var element = document.createElement('div');
element.innerHTML = "Héllo world!";

document.getElementById('demo1').value = element.textContent;
element.textContent = '';
<input type="text" id="demo1" />
Community
  • 1
  • 1
Maarten Bicknese
  • 1,498
  • 1
  • 14
  • 27
0

You have to escape the ampersand with &amp;:

document.write("&amp;eacute;");
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177