Skip to content

Instantly share code, notes, and snippets.

@byverdu
Last active May 22, 2020 09:28
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 byverdu/869f6735a56c45967bf705d98e591b50 to your computer and use it in GitHub Desktop.
Save byverdu/869f6735a56c45967bf705d98e591b50 to your computer and use it in GitHub Desktop.
const allInputs = document.querySelectorAll( 'a' );
const urlToSearch = 'https://github.com/byverdu';
let found;
let position;
// Ways to iterate over a NodeList to find an element
// 1
allInputs.forEach(( link, index ) => {
if (link.href.indexOf( urlToSearch ) !== -1 ) {
position = index;
}
});
found = allInputs[ position ];
// 2
for ( let i = 0; i <= allInputs.length; i++ ) {
if (allInputs[ i ].href.indexOf( urlToSearch ) !== -1 ) {
position = index;
}
}
found = allInputs[ position ];
// 3
found = [].find.call( allInputs, link => link.href === urlToSearch );
// 4
found = Array.from( allInputs ).find( link => link.href === urlToSearch );
//5
found = [...allInputs].find(link => link.href === urlToSearch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment