Created
April 22, 2012 00:41
-
-
Save Davis-Desormeaux/2440544 to your computer and use it in GitHub Desktop.
Quick Dom Selector
This file contains hidden or 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
function domSelect(element) { | |
var elm_type = element.substring(0, 1); | |
var _element = element.substring(1); | |
return elm_type != '.' | |
? elm_type != '#' | |
? document.getElementsByName(element) // Default to name. | |
: document.getElementById(_element) // By an id request. | |
: document.getElementsByClassName(_element) // By class request. | |
} | |
// Example: | |
alert(domSelect('#SomeIdValue').src) // Could be an image src | |
alert(domSelect('.SomeClassValue')[0].value) // 1st Element Found By Class | |
alert(domSelect('SomeTagValue')[0].value) // 1st Element Found By Name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment