Skip to content

Instantly share code, notes, and snippets.

@danielhaim1
Last active March 19, 2023 22:07
Show Gist options
  • Save danielhaim1/5731454 to your computer and use it in GitHub Desktop.
Save danielhaim1/5731454 to your computer and use it in GitHub Desktop.
The JavaScript function replaces all occurrences of the "&" character in the text content of all elements in the document with the HTML entity "&".
document.addEventListener('DOMContentLoaded', () => {
Array.from(document.querySelectorAll('*:contains("&")')).forEach((elem) => {
Array.from(elem.childNodes).forEach((node) => {
if (node.nodeType === Node.TEXT_NODE) {
const newNode = document.createElement('span');
newNode.className = 'amp';
newNode.textContent = node.textContent.replace(/&/g, '&');
node.parentNode.replaceChild(newNode, node);
}
});
});
});
span.amp {
font-family: Baskerville, "Baskerville Old Face", "Hoefler Text", Garamond, "Times New Roman", serif;
font-style: italic;
font-weight: normal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment