Skip to content

Instantly share code, notes, and snippets.

@sirbrad
Created April 25, 2012 15:23
Show Gist options
  • Save sirbrad/2490603 to your computer and use it in GitHub Desktop.
Save sirbrad/2490603 to your computer and use it in GitHub Desktop.
Finding specific elements
/*IE8+*/
function findElem(elem, va, arg, str){
var el = document.getElementsByTagName(elem),
len = el.length;
while (len--) {
// document.body["getAttribute"].call(elementToUseInstead, arg1, arg2, arg3)
// document.body["getAttribute"].apply(elementToUseInstead, [arg1, arg2, arg3])
if (document.body[va].call(el[len], arg) === str) {
return el[len];
}
}
};
//Demo
findElem('type of element', 'method', 'attribute', 'value');
// Real world use
findElem('form', 'getAttribute', 'data-target', 'value')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment