Created
July 25, 2019 05:13
-
-
Save joshuacerbito/a283ea644293b5540fab410bf3d0e559 to your computer and use it in GitHub Desktop.
Sets multiple html element attributes at once
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
/** | |
* 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