How to preserve HTML entities in JS string?

Viewed 170

I have text content in an element that uses HTML entities for quotes. I need to get this content and insert it into a data attribute on another element, which of course requires the quotes to remain escaped. But no matter what I do, JS keeps converting the entities to characters. How can I prevent this?

let lmnt = document.getElementById('lmnt');

console.log(lmnt.textContent);
console.log(lmnt.innerHTML);
console.log(lmnt.innerText);
<html>
<body>
<h1 id="lmnt">Robert &quot;Bob&quot; Robertson</h1>
</body>
</html>

0 Answers
Related