Skip to content

Instantly share code, notes, and snippets.

@itzarty
Last active February 25, 2023 22:25
Show Gist options
  • Select an option

  • Save itzarty/a33924b3a94fb707439700ac46823621 to your computer and use it in GitHub Desktop.

Select an option

Save itzarty/a33924b3a94fb707439700ac46823621 to your computer and use it in GitHub Desktop.
const $n = ( name, appendTo, additionalProps, innerHTML ) => {
const element = document.createElement( name );
if( typeof appendTo == 'object' ) appendTo.appendChild( element );
if( typeof appendTo == 'string' ) document.querySelector( appendTo ).appendChild( element );
if( typeof additionalProps == 'object' ) {
for( const i in additionalProps ) {
element.setAttribute( i, additionalProps[ i ] );
}
}
if( innerHTML ) element.innerHTML = innerHTML;
return element;
}
@itzarty
Copy link
Author

itzarty commented Jun 24, 2022

This function allows you to use the document.createElement() function much more effectivelly with the feature of setting element attributes in the same block.

Usage: $n( Element name, Where should element be appended - supports direct element & query, Additional properties - attributes, innerHTML )

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