Skip to content

Instantly share code, notes, and snippets.

@mattConn
Last active March 11, 2017 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattConn/32251b60dedf1527847d9ca0f73421bc to your computer and use it in GitHub Desktop.
Save mattConn/32251b60dedf1527847d9ca0f73421bc to your computer and use it in GitHub Desktop.
Get all text elements in a document.
// simple tag getting fn
var getTag = function(tag){
return document.getElementsByTagName(tag);
}
// wanted: elements we want to get by tag.
// caught: tags caught; where we will put the wanted elements, if they exist.
// missing: if tag not present in document, list here.
var textCatcher = {
wanted : ['p','ul','ol','li','a','span','strong','i','b','h1','h2','h3','h4','h5','h6'],
caught : [],
missing: []
}
// Search document for all wanted tags.
// If the wanted tag exists in document,
// push all elements of that tag into 'caught' array.
// Else, push tag name (listed in 'wanted' array) into missing array.
for( var i=0; i < textCatcher.wanted.length; i++ ){
if( getTag( textCatcher.wanted[i]).length > 0 ){
textCatcher.caught.push( getTag( textCatcher.wanted[i] ) );
} else {
textCatcher.missing.push( textCatcher.wanted[i] );
}
}
// log to console for inspection
console.log(textCatcher);
@mattConn
Copy link
Author

mattConn commented Mar 11, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment