Last active
March 19, 2023 22:07
-
-
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 "&".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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