Last active
October 8, 2015 04:48
-
-
Save craiga/3279884 to your computer and use it in GitHub Desktop.
getElementId
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
/** | |
* Get the ID of an element. | |
* | |
* Get the ID of an element, generating one if none exists and the second | |
* parameter is set to true. | |
* Makes use of {@link https://gist.github.com/craiga/4075392 randomString}. | |
* | |
* @author Craig Anderson <[email protected]> | |
* @link https://gist.github.com/3279884 | |
*/ | |
function getElementId(ele, generateId) { | |
if(!ele.hasAttribute("id") && generateId) { | |
do { | |
generatedId = randomString(); | |
} while(document.getElementById(generatedId)) | |
ele.setAttribute("id", generatedId); | |
} | |
return ele.getAttribute("id"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment