There's a difference between html and text. You want to set text:
var myText = 'A & B';
// Setting HTML: Result text will be "A & B"
document.write(myText); // <- don't use document.write!
jQuery('#myElement').html(myText);
myElement.innerHTML = myText;
// Setting Text: Result text will be "A & B"
document.write('A & B'.replace(/&/g, '&').replace(/</g, '<')); // <- don't use document.write!
jQuery('#myElement').text(myText); // removes all children and sets the text of the element
myElement.textContent = myText; // sets the text of the element
myElement.innerText = myText; // removes all children and sets the text of the element
Note that document.write
is usually a bad idea, since it only works when your page was not loaded completely, and using document.write
later (like when clicking a button) will replace the entire content of your page.
I can also advise against using innerText
. It is a non-standard property which was defined by Internet Explorer and later adapted by Chrome, but is not supported in Firefox (which is fine, seeing as it is not in the standards). You can use textContent
instead.