Skip to content

Instantly share code, notes, and snippets.

@JHarry444
Last active June 19, 2020 11:19
Show Gist options
  • Save JHarry444/2afd89822cc7e868c867460a8aa7edb2 to your computer and use it in GitHub Desktop.
Save JHarry444/2afd89822cc7e868c867460a8aa7edb2 to your computer and use it in GitHub Desktop.
JS function that create an HTML Element and then sets its attributes.
function makeElement(eleType, appendTo, attributes) {
const element = document.createElement(eleType);
for (let attr in attributes) {
element[attr] = attributes[attr];
}
appendTo.appendChild(element);
return element;
}
@JHarry444
Copy link
Author

e.g.
makeElement('p', body, { 'innerText': 'bloop', style: 'color: blue'});
Would produce
<p style=​"color:​ blue;​">​bloop​</p>​

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment