Say I have this HTML element:
<td>—</td>
When parsed by browsers, —
is converted to an actual em-dash, like so:
<td>—</td>
How can I test for —
without using other characters in my JavaScript code?
console.log(elem.innerHTML == "—"); // false
console.log(elem.textContent == "—"); // false
console.log(elem.innerHTML == "—"); // true
console.log(elem.textContent == "—"); // true
You could create a new DOM element, and compare the two:
/**
* Test that a DOM element's inner HTML is === —
*/
function mdashTest(el) {
var tempEl = document.createElement('div');
tempEl.innerHTML = '—';
return el.innerHTML === tempEl.innerHTML;
}
// Test it!
elem = document.getElementById('dash');
alert( mdashTest( elem ) );
<div id="dash">—</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With