Skip to content

Instantly share code, notes, and snippets.

@craiga
Last active October 8, 2015 04:48
Show Gist options
  • Save craiga/3279884 to your computer and use it in GitHub Desktop.
Save craiga/3279884 to your computer and use it in GitHub Desktop.
getElementId
/**
* 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