- appendAfter()
- appendHTML()
- attributeJSON()
- getElementTagType()
- getPropertyValues()
- prependChild()
- uniqid()
- requireUniqid()
- unwrap()
- wrap()
appendAfter - Append node after a specific node
appendAfter( newNode, referenceNode ) // voidAppends a node after a specified node.
| Parameter | Type | Default | Description |
|---|---|---|---|
| newNode | Node | - | The node to append |
| referenceNode | Node | - | The node after which we want to append |
| Type/Value | Description |
|---|---|
| void | None. |
appendAfter( document.getElementById( 'move-me' ), document.getElementById( 'after-this' ) );appendHTML - Append HTML string as one or multiple elements
appendHTML( element, str ) // voidAppend HTML string as one or multiple elements, uses str2node() to convert the string.
| Parameter | Type | Default | Description |
|---|---|---|---|
| element | HTMLElement | - | Element to append to |
| str | String | - | The html string |
| Type/Value | Description |
|---|---|
| void | Node. |
appendHTML( document.getElementById( 'target' ), '<section>...</section>' );attributeJSON - Get json object from element data attribute
attributeJSON( name, element, silent = true ) // ObjectGet json object from element data attribute, can optionally throw exceptions on error.
| Parameter | Type | Default | Description |
|---|---|---|---|
| name | String | - | Attribute name |
| element | HTMLElement | - | Element to read |
| silent | Boolean | true | False to throw error on parse Error |
| Type/Value | Description |
|---|---|
| null | Invalid or empty attribute |
| Object | Parsed json object data |
<div id="json-data" data-config='{"foo":1}'></div>attributeJSON( 'data-config', document.getElementById( 'json-data' ) ); // { foo : 1 }getElementTagType - Get element tag type string
getElementTagType( element ) // stringGet element tag type string, compiled from tagName + type.
| Parameter | Type | Default | Description |
|---|---|---|---|
| element | HTMLElement | - | Element to get type string from |
| Type/Value | Description |
|---|---|
| String | Tag type string |
getElementTagType( document.getElementById( 'input[type="hidden"]' ) ); // input-hiddengetPropertyValues - Get custom property values from given context
getPropertyValues( values, context = null, assoc = true ) // string|Object|Array<string>Get values from custom properties in given context, returns the actual computed value.
| Parameter | Type | Default | Description |
|---|---|---|---|
| values | string/Array | - | Property name/s excluding the double dash |
| context | Body/HTMLElement | null/:root | Context to read values from, default: html/root element |
| assoc | boolean | true | Return associative object with property names |
| Type/Value | Description |
|---|---|
| String | Single property value |
| Object | Associative object with props |
| Array | List of values |
:root {
--prop-name: 10px;
}getPropertyValues( 'prop-name' ); // '10px'prependChild - Prepend node to node
prepend( newNode, referenceNode ) // voidPrepend node as child of node, newNode is inserted before the firstChild of referenceNode.
| Parameter | Type | Default | Description |
|---|---|---|---|
| newNode | HTMLElement | - | Element to prepend |
| referenceNode | HTMLElement | - | Element to prepend to |
| Type/Value | Description |
|---|---|
| void | None. |
prepend( document.getElementById( 'node-to-insert' ), document.getElementById( 'node-to-prepend-to' ) );uniqid - Unique html attribute id
uniqid( prefix = '', entropy = false ) // stringGet a unique html attribute id that is unused.
| Parameter | Type | Default | Description |
|---|---|---|---|
| prefix | String | '' | Prefix the id with given string |
| entropy | Boolean | false | Increase the id entropy |
| Type/Value | Description |
|---|---|
| String | Unique unused string id |
uniqid( 'prefix-' ); // 'prefix-npnm623gm2'requireUniqid - Require unique html attribute id
requireUniqid( element, prefix = '', entropy = false ) // stringGet a unique html attribute id that is unused.
| Parameter | Type | Default | Description |
|---|---|---|---|
| element | HTMLElement | - | Element that requires a unique id |
| prefix | String | '' | Prefix the id with given string |
| entropy | Boolean | false | Increase the id entropy |
| Type/Value | Description |
|---|---|
| String | Unique string id set for the element |
requireUniqid( document.querySelector( '.requires-id' ), 'prefix-' ); // 'prefix-npnm623gm2'unwrap - Unwrap element
unwrap( element ) // voidRemoves the given element and preserves any children.
| Parameter | Type | Default | Description |
|---|---|---|---|
| element | HTMLElement | - | Element wrapper to remove |
| Type/Value | Description |
|---|---|
| void | None. |
unwrap( document.getElementById( 'element-to-unwrap' ) ); // voidwrap - Wrap element/s
unwrap( elements, wrapper = 'div', strict = true ) // HTMLElementWraps given element or elements with a given element
| Parameter | Type | Default | Description |
|---|---|---|---|
| elements | HTMLElement/NodeList | - | Element/s to wrap |
| wrapper | HTMLElement/String | 'div' | Element to wrap around elements |
| strict | Boolean | true | Throw error if wrapper is already connected |
| Type/Value | Description |
|---|---|
| HTMLElement | The wrapper element reference |
wrap( document.querySelectorAll( '.selector' ) ); // HTMLDivElement