Skip to content

Instantly share code, notes, and snippets.

@joshuacerbito
Created July 25, 2019 05:13
Show Gist options
  • Save joshuacerbito/a283ea644293b5540fab410bf3d0e559 to your computer and use it in GitHub Desktop.
Save joshuacerbito/a283ea644293b5540fab410bf3d0e559 to your computer and use it in GitHub Desktop.
Sets multiple html element attributes at once
/**
* setAttributes
* Sets multiple html element attributes at once
*
* @param elm html element that the at attributes will be applied to
* @param attrs object with key (attribute name) and value (attribute value)
*
* e.g.
* setAttributes(document.querySelector('.sample'), {
* 'title': 'Sample Title',
* 'tabindex': 0
* });
*/
function setAttributes(elm = {}, attrs = {}) {
if (Object.keys(attrs).length > 0) {
Object.keys(attrs).forEach(attr => {
elm.setAttribute(attr, attrs[attr]);
});
}
}
export { setAttributes };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment