Skip to content

Instantly share code, notes, and snippets.

@AhmedElwerdany
Last active October 1, 2022 11:07
Show Gist options
  • Save AhmedElwerdany/c32db4881491b87d7fb17d940ea14595 to your computer and use it in GitHub Desktop.
Save AhmedElwerdany/c32db4881491b87d7fb17d940ea14595 to your computer and use it in GitHub Desktop.
getting the text of HTML element excluding children
/*
* <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