Reputation: 978
When setting placeholder text via javascript, special characters are not displaying properly.
function myFunction() {
document.getElementById("myText").placeholder = "Événement";
}
<input type="text" id="myText" placeholder="Événement">
<p>Click the button to change the placeholder text of the text field.</p>
<button onclick="myFunction()">Try it</button>
Output is: "Événement"
Expected output: "Événement"
Looking for a javascript solution where I can convert any text containing any encoded character.
Upvotes: 2
Views: 3089
Reputation: 757
The most straightforward way to convert is to create an element, set your HTML entity as innerHTML and get textContent, something like this:
function myFunction() {
let a = document.createElement('span');
a.innerHTML = 'Événement from JS';
document.getElementById("myText").placeholder = a.textContent;
}
<input type="text" id="myText" placeholder="Événement">
<p>Click the button to change the placeholder text of the text field.</p>
<button onclick="myFunction()">Try it</button>
Upvotes: 2
Reputation: 7782
In HTML, special characters are encoded with &#XXX
(Like É
).
In Javascript, special characters are encoded with \uXXXX
(Like \u00C9
)
Check https://en.wikipedia.org/wiki/List_of_Unicode_characters for the list of escape code.
In your case, it would be
document.getElementById("myText").placeholder = "\u00C9v\u00E8nement";
Upvotes: 4