This function creates DOM elements with styles, attributes, content and let you insert them in the DOM through multiple options.
createElement([option, insert]);
import createElement from 'create-element';
let el = createElement({
type: 'a',
link: 'image.png',
style: 'my-first-style my-second-style',
attributes: {'target': '_blank', 'title': 'this is awesome'},
content: 'click here'
},{
target: document.getElementById('target'),
type: 'sibling',
method: 'after'
});
This function has two optionals parameters you can use with object type argument. The first one is set in order to define element configuration wherheas the second one is used in order to configure insertion in the DOM. Several optional properties can be set inside of them:
option | type | explanation | exemple |
---|---|---|---|
type - default: 'div' | String | Element type you want to create |
|
id | String | Id that you may want to set on the element. |
|
style | String | You can set one or multiple classes by adding them in string separated by space. |
|
Object | You can also set inline style using an object listing all the style you want to apply, separated by comma. |
|
|
attributes | Object | List of attributes to apply. Think about `target` or `title` for <a> element or `alt` for <img> element. Data attributes can also be set. |
|
src | String | Link to the image for src attribute on media elements. |
|
link | String | Target you want to link with href attribute on link elements (internal or external links are OK) |
|
content | String | String that will be added to the content of the new element. |
|
HTMLNode | Node that will be added to the content of the new element |
|
option | type | value | explanation | exemple |
---|---|---|---|---|
target | HTMLNode | Target element that is the reference for adding the new element in the DOM. |
|
|
type - default: 'sibling' | String | sibling | The new element will be adding as a sibling of the targeted element. |
|
container | The new element will be adding as a child of targeted element. |
|
||
method default: 'after' | String | before | The new element will be added before the target in case of sibling type or as a first child of the target in case of container type. |
|
after | The new element will be added after the target in case of sibling type or as a last child of the target in case of container type. |
|