Skip to content

Instantly share code, notes, and snippets.

@johnnyferreiradev
Created September 24, 2020 16:39
Show Gist options
  • Save johnnyferreiradev/7971bc0c09bb772b3b16661f67c08e25 to your computer and use it in GitHub Desktop.
Save johnnyferreiradev/7971bc0c09bb772b3b16661f67c08e25 to your computer and use it in GitHub Desktop.
Function for creating DOM elements
const createElement = (elementName, attributes) => {
const element = document.createElement(elementName);
const attributeAsArray = Object.entries(attributes);
attributeAsArray.forEach(([key, value]) => element.setAttribute(key, value));
return element;
};
// Example of use
const input = createElement('input', {
type: 'text',
value: 'teste',
id: 'input1',
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment