Last active
February 25, 2023 22:25
-
-
Save itzarty/a33924b3a94fb707439700ac46823621 to your computer and use it in GitHub Desktop.
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
| 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; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 )