Last active
October 1, 2022 11:07
-
-
Save AhmedElwerdany/c32db4881491b87d7fb17d940ea14595 to your computer and use it in GitHub Desktop.
getting the text of HTML element excluding children
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
/* | |
* <div id='element'>hello world <span>!</span></div> | |
* | |
*/ | |
const element = document.querySelector('#element'); | |
let result = ''; | |
element.childNodes.forEach(child => { | |
if (child.nodeName === '#text') { | |
result += child.textContent; | |
} | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment