Manual Reference Source Test
import HtmlElement from 'elementify/lib/elements/htmlelement.js'
public class | version 1.0.0 | since 1.0.0 | source

HtmlElement

Extends:

elements/eventifiedelement~EventifiedElement → HtmlElement

This the common class for all other elements. It provides all functionalities to :

  • Explore and manipulate the DOM
  • Create, edit, delete properties on the object
  • Style the object
  • Fade in and out
  • Get and set position
  • Add event with a global events manager

    You cannot directly create an instance of the class.

    You must use the Element constructor or its alias Q that will then instantiate the right class given the node type (HTML, Form...).

    The options passed to the Element constructor are then passed to this class constructor.

Constructor Summary

Private Constructor
private

constructor(node: Element, options: keyValueObject): HtmlElement

Constructor for HTMLElement class

Member Summary

Public Members
public set
public get

bottom: *

Get/Set bottom offset in pixels (relative to wiewport) You can set value by assigning any of :

version 1.0.0 since 1.0.0
public

The underlying DOM node object

since 1.0.0
public

Convenience property to check if the object is an instance of this class

since 1.0.0
public get

height: *

Get/Set height in pixels You can set value by assigning any of :

version 1.0.0 since 1.0.0
public set
public get

Check if the HtmlElement is present in DOM

version 1.0.0 since 1.0.0
public get

Get left offset in pixels (relative to wiewport)

version 1.0.0 since 1.0.0
public set

Set left offset in pixels (relative to wiewport)

version 1.0.0 since 1.0.0
public

The length property if useful to quickly check what kind of underlying node object have been returned :

  • 0 means it's a DocumentFragment
  • 1 means it's a node (HTMLElement...)
  • More than 1 means it's a Collection (@see elementify.Collection)
since 1.0.0
public get

Alias for the element property member that returns null instead of undefined

version 1.0.0 since 1.0.0
public

The options passed to the constructor

since 1.0.0
public get

Get the outerHTML string of the underlying node

version 1.0.0 since 1.0.0
public get

Get the position property of the element.

version 1.0.0 since 1.0.0
public get

right: *

Get/Set right offset in pixels (relative to wiewport) You can set value by assigning any of :

version 1.0.0 since 1.0.0
public set
public get

root: Document | Node

Get the root Node element for the HtmlElement The result is not enhanced as an HtmlElement because it will mostly be a document object which is not a Node object.

version 1.0.0 since 1.0.0
public get

Get scroll X offset of the window in pixels

version 1.0.0 since 1.0.0
public get

Get scroll Y offset of the window in pixels

version 1.0.0 since 1.0.0
public get

styles: CSSStyleDeclaration

Get the computed styles of the underlying node.

version 1.0.0 since 1.0.0
public get

top: *

Get/Set top offset in pixels (relative to wiewport) You can set value by assigning any of :

version 1.0.0 since 1.0.0
public set

top

public get

Get the visibility of an HtmlElement.

version 1.0.0 since 1.0.0
public get

width: *

Get/Set width in pixels You can set value by assigning any of :

version 1.0.0 since 1.0.0
public set
Private Members
private

_metrics: {}

Method Summary

Public Methods
public

Add class(es) to an HtmlElement

version 1.0.0 since 1.0.0
public

Insert an element before another.

version 1.0.0 since 1.0.0
public

Append an element to another

version 1.0.0 since 1.0.0
public

attr(a: Void | String | Array | keyValueObject, v: Void | Null | Number | String): HtmlElement | Number | String | keyValueObject

Get or set given attribute(s) of the element.

version 1.0.0 since 1.0.0
public

Insert an element before another.

version 1.0.0 since 1.0.0
public

Element's nth child finder

version 1.0.0 since 1.0.0
public

Element's childs finder based on a DOM query selector

version 1.0.0 since 1.0.0
public

Returns a deep clone of the HtmlElement.

version 1.0.0 since 1.0.0
public

Get or set data attribute(s) of the element.

version 1.0.0 since 1.0.0
public

Set the display style property of an HtmlElement

version 1.0.0 since 1.0.0
public

Empty an Element

version 1.0.0 since 1.0.0
public

fadeIn(options: ObjectArray, callback: Callback): this | Promise

Fade In element given a linear easing

version 1.0.0 since 1.0.0
public

fadeOut(options: ObjectArray, callback: Callback): this | Promise

Fade Out element given a linear easing

version 1.0.0 since 1.0.0
public

forEach(callback: forEachCallback): void

version 1.0.0 since 1.0.0
public

Check if an HtmLElement have given class(es)

version 1.0.0 since 1.0.0
public

Hide an HtmlElement by setting its display property to 'none'.

version 1.0.0 since 1.0.0
public

html(v: String): HTMLElement | string

Get/Set the HTML content of the node

version 1.0.0 since 1.0.0
public

matches(selector: String): Boolean

Matches returns true if the element matches the query selector

version 1.0.0 since 1.0.0
public

Element's next sibling finder

version 1.0.0 since 1.0.0
public

Element's next siblings finder based on a DOM query selector

version 1.0.0 since 1.0.0
public

Element's n-th parent finder

version 1.0.0 since 1.0.0
public

Element's parent finder based on a DOM query selector

version 1.0.0 since 1.0.0
public

Prepend an element to another

version 1.0.0 since 1.0.0
public

Element's previous sibling finder

version 1.0.0 since 1.0.0
public

Element's previous siblings finder based on a DOM query selector

version 1.0.0 since 1.0.0
public

Remove an Element

version 1.0.0 since 1.0.0
public

Remove class(es) from an HtmlElement

version 1.0.0 since 1.0.0
public

Returns a shallow copy of the HtmlElement

version 1.0.0 since 1.0.0
public

Show an HtmlElement by setting its display property.

version 1.0.0 since 1.0.0
public

Get/Set style properties of an HtmlElement based on its node style attribute.

version 1.0.0 since 1.0.0
public

Toggle an HtmlElement visibility by setting its display property.

version 1.0.0 since 1.0.0
public

Toggle class(es) from an HtmlElement

version 1.0.0 since 1.0.0
public

Unwrap an Element (and its siblings) and delete its parentNode

version 1.0.0 since 1.0.0
public

wrap(e: String | Node | HtmlElement, returnself: Boolean): HtmlElement

Wrap an Element into another.

version 1.0.0 since 1.0.0
Private Methods
private

_metricsUpdate(): void

Refresh position object at first call or after a resize / changeOrientation event

version 1.0.0 since 1.0.0
private

_parseDims(v: *): *

private

Private Constructors

private constructor(node: Element, options: keyValueObject): HtmlElement source

Constructor for HTMLElement class

You can pass any attributes as options. There are a few reserved keywords for specific options.

Params:

NameTypeAttributeDescription
node Element

Element to enhance

options keyValueObject
  • optional
  • default: {}

Options for the HTMLElement object and/or attributes for the underlying native HTMLElement Object. Excepting data, events, innerhtml and options, any property of this object will be added as attribute to the underlying HTML object.

Return:

HtmlElement

New HTMLElement object

Return Properties:

NameTypeAttributeDescription
options.class String | Array
  • optional

Class(es) to be set as value for the class attribute.

options.style String | keyValueObject
  • optional

Style(s) to be set as value for the style attribute.

options.data keyValueObject
  • optional

An object with key/value pairs

options.events keyValueObject
  • optional

Events to add to the element under the format : {eventName: callback, ...}

Public Members

public set bottom source

public get bottom: * version 1.0.0 since 1.0.0 source

Get/Set bottom offset in pixels (relative to wiewport) You can set value by assigning any of :

  • null, undefined : Remove value
  • {Number} : Value will be interpreted in pixels
  • {String} : any css acceptable value for the property (e.g '5vi', '1em'...)

See:

public element: Node since 1.0.0 source

The underlying DOM node object

public enhanced: Boolean since 1.0.0 source

Convenience property to check if the object is an instance of this class

public get height: * version 1.0.0 since 1.0.0 source

Get/Set height in pixels You can set value by assigning any of :

  • null, undefined : Remove value
  • {Number} : Value will be interpreted in pixels
  • {String} : any css acceptable value for the property (e.g '5vi', '1em'...)

See:

public set height source

public get isInDom: Boolean version 1.0.0 since 1.0.0 source

Check if the HtmlElement is present in DOM

public get left: Number | undefined version 1.0.0 since 1.0.0 source

Get left offset in pixels (relative to wiewport)

See:

public set left version 1.0.0 since 1.0.0 source

Set left offset in pixels (relative to wiewport)

You can set value by assigning any of :

  • null, undefined : Remove value
  • {Number} : Value will be interpreted in pixels
  • {String} : any css acceptable value for the property (e.g '5vi', '1em'...)

See:

public length: Number since 1.0.0 source

The length property if useful to quickly check what kind of underlying node object have been returned :

  • 0 means it's a DocumentFragment
  • 1 means it's a node (HTMLElement...)
  • More than 1 means it's a Collection (@see elementify.Collection)

public get node: Node version 1.0.0 since 1.0.0 source

Alias for the element property member that returns null instead of undefined

See:

public options: ObjectArray since 1.0.0 source

The options passed to the constructor

public get outerHTML: String version 1.0.0 since 1.0.0 source

Get the outerHTML string of the underlying node

public get position: NodeMetrics version 1.0.0 since 1.0.0 source

Get the position property of the element. The calculation only occurs at first call or when window is resized, rotated or scrolled.

Warning : Calculations can be pretty slow on huge amount of elements

A cached version of the result is available in the _metrics property of the HtmlElement. It will be only be generated after a first call to position or another getter/setter. It will then be regenerated after events that affect window size or scroll

If the element is not in DOM, all values will return undefined.

public get right: * version 1.0.0 since 1.0.0 source

Get/Set right offset in pixels (relative to wiewport) You can set value by assigning any of :

  • null, undefined : Remove value
  • {Number} : Value will be interpreted in pixels
  • {String} : any css acceptable value for the property (e.g '5vi', '1em'...)

See:

public set right source

public get root: Document | Node version 1.0.0 since 1.0.0 source

Get the root Node element for the HtmlElement The result is not enhanced as an HtmlElement because it will mostly be a document object which is not a Node object.

public get scrollX: Number | undefined version 1.0.0 since 1.0.0 source

Get scroll X offset of the window in pixels

See:

public get scrollY: Number | undefined version 1.0.0 since 1.0.0 source

Get scroll Y offset of the window in pixels

See:

public get styles: CSSStyleDeclaration version 1.0.0 since 1.0.0 source

Get the computed styles of the underlying node. Use with care : It can be painfully slow and must be avoided for huge set of automated ops on style

public get top: * version 1.0.0 since 1.0.0 source

Get/Set top offset in pixels (relative to wiewport) You can set value by assigning any of :

  • null, undefined : Remove value
  • {Number} : Value will be interpreted in pixels
  • {String} : any css acceptable value for the property (e.g '5vi', '1em'...)

See:

public set top source

public get visible: Number version 1.0.0 since 1.0.0 source

Get the visibility of an HtmlElement. It will only return false if the element is out of the DOM or with a display style property set to none.

Use with care : It can be painfully slow and must be avoided for huge set of automated ops on elements if display property is not set inline

public get width: * version 1.0.0 since 1.0.0 source

Get/Set width in pixels You can set value by assigning any of :

  • null, undefined : Remove value
  • {Number} : Value will be interpreted in pixels
  • {String} : any css acceptable value for the property (e.g '5vi', '1em'...)

See:

public set width source

Private Members

private _metrics: {} source

Public Methods

public addClass(cls: String): HtmlElement version 1.0.0 since 1.0.0 source

Add class(es) to an HtmlElement

Params:

NameTypeAttributeDescription
cls String

Class(es) name(s)

Return:

HtmlElement

Self for chaining

public after(e: String | Node | HtmlElement, returnself: Boolean): HtmlElement version 1.0.0 since 1.0.0 source

Insert an element before another. A Documentfragement wrapper will be inserted if Element have no parent node.

The second parameter can be used to get the parent element for chaining instead of the element itself.

Params:

NameTypeAttributeDescription
e String | Node | HtmlElement

Element to insert

returnself Boolean
  • optional
  • default: true

If true, it will return the Element. If false, it will return the parent Element.

Return:

HtmlElement

Return for chaining

public append(e: String | Node | HtmlElement): HtmlElement version 1.0.0 since 1.0.0 source

Append an element to another

Params:

NameTypeAttributeDescription
e String | Node | HtmlElement

Element to append (enhanced or not)

Return:

HtmlElement

Return self for chaining

public attr(a: Void | String | Array | keyValueObject, v: Void | Null | Number | String): HtmlElement | Number | String | keyValueObject version 1.0.0 since 1.0.0 source

Get or set given attribute(s) of the element.

The getter can be used in two ways :

  • A string will return an attribute value
  • Array of attributes names will return a keyValueObject

The setter can be used in three ways :

  • A string + value will set the attribute
  • A string + null will delete the attribute
  • A keyValueObject of attributes/values for multiple settings

Params:

NameTypeAttributeDescription
a Void | String | Array | keyValueObject
  • optional

Attribute(s)

v Void | Null | Number | String

Value for the attribute

Return:

HtmlElement | Number | String | keyValueObject

Self for chaining for setter or attributes for getter

public before(e: String | Node | HtmlElement, returnself: Boolean): HtmlElement version 1.0.0 since 1.0.0 source

Insert an element before another. A Documentfragement wrapper will be inserted if Element have no parent node.

The second parameter can be used to get the parent element for chaining instead of the element itself.

Params:

NameTypeAttributeDescription
e String | Node | HtmlElement

Element to insert

returnself Boolean
  • optional
  • default: true

If true, it will return the Element. If false, it will return the parent Element.

Return:

HtmlElement

Return for chaining

public child(n: Number): HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source

Element's nth child finder

Params:

NameTypeAttributeDescription
n Number
  • optional
  • default: 1

Distance to the current Element

Return:

HtmlElement | DocumentFragment

If the child is unreachable, it will returns a DocumentFragment

public childs(selector: String): Collection | HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source

Element's childs finder based on a DOM query selector

Params:

NameTypeAttributeDescription
selector String

A valid DOM query selector

Return:

Collection | HtmlElement | DocumentFragment

HtmlElement or DocumentFragment if no child matches the selector

public clone(): HtmlElement version 1.0.0 since 1.0.0 source

Returns a deep clone of the HtmlElement. The clone will contains the same child nodes.

Return:

HtmlElement

Clone of the current HtmlElement

public data(a: undefined | String | Array | Object | ObjectArray, v: Number | String | Object): HtmlElement | Number | String | Object | keyValueObject version 1.0.0 since 1.0.0 source

Get or set data attribute(s) of the element.

With no arguments, it will return a keyValueObject with all data attributes.

Data items name will be automatically converted from dash to camel-case if needed.

Params:

NameTypeAttributeDescription
a undefined | String | Array | Object | ObjectArray
  • optional

Data attribute name (without leading data-)

v Number | String | Object

Value for the attribute

Return:

HtmlElement | Number | String | Object | keyValueObject

Self for chaining for setter or data attribute(s) values for getter

public display(v: String): HtmlElement version 1.0.0 since 1.0.0 source

Set the display style property of an HtmlElement

Params:

NameTypeAttributeDescription
v String
  • optional

Value for display property

Return:

HtmlElement

Self for chaining

public empty(): HtmlElement version 1.0.0 since 1.0.0 source

Empty an Element

Return:

HtmlElement

Return for chaining

public fadeIn(options: ObjectArray, callback: Callback): this | Promise version 1.0.0 since 1.0.0 source

Fade In element given a linear easing

Params:

NameTypeAttributeDescription
options ObjectArray

Options for the animation

options.display String
  • optional
  • default: 'block'

Value for the display CSS property

options.duration Number
  • optional
  • default: 400

Duration of the animation in milliseconds

callback Callback

Animation callback

Return:

this | Promise

Chainable or animation promise if available

public fadeOut(options: ObjectArray, callback: Callback): this | Promise version 1.0.0 since 1.0.0 source

Fade Out element given a linear easing

Params:

NameTypeAttributeDescription
options ObjectArray

Options for the animation

options.display String
  • optional
  • default: 'none'

Value for the display CSS property at the end of the animation

options.duration Number
  • optional
  • default: 400

Duration of the animation in milliseconds

callback Callback

Animation callback

Return:

this | Promise

Chainable or animation promise if available

public forEach(callback: forEachCallback): void version 1.0.0 since 1.0.0 source

Params:

NameTypeAttributeDescription
callback forEachCallback

Callback function to be run on each element.

Return:

void

public hasClass(cls: String): Boolean version 1.0.0 since 1.0.0 source

Check if an HtmLElement have given class(es)

Params:

NameTypeAttributeDescription
cls String

Class(es) name(s)

Return:

Boolean

True if class(es) are present

public hide(): HtmlElement version 1.0.0 since 1.0.0 source

Hide an HtmlElement by setting its display property to 'none'.

Return:

HtmlElement

Self for chaining

public html(v: String): HTMLElement | string version 1.0.0 since 1.0.0 source

Get/Set the HTML content of the node

Params:

NameTypeAttributeDescription
v String
  • optional

Value for HTML content

Return:

HTMLElement | string

Setter returns self for chaining, getter returns the string

public matches(selector: String): Boolean version 1.0.0 since 1.0.0 source

Matches returns true if the element matches the query selector

Params:

NameTypeAttributeDescription
selector String

Valid DOM query selector

Return:

Boolean

true if Element matches selector

public nextSibling(n: Qtype): HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source

Element's next sibling finder

Params:

NameTypeAttributeDescription
n Qtype
  • optional
  • default: 1

Distance to current Element

Return:

HtmlElement | DocumentFragment

If the sibling is unreachable, it will returns a DocumentFragment

public nextSiblings(selector: String): Collection | HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source

Element's next siblings finder based on a DOM query selector

Params:

NameTypeAttributeDescription
selector String

A valid DOM query selector

Return:

Collection | HtmlElement | DocumentFragment

DocumentFragment if no siblings matches the selector

public parent(n: Number | String): HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source

Element's n-th parent finder

Params:

NameTypeAttributeDescription
n Number | String
  • optional
  • default: 1

Distance to the current Element if number. Query selector if string.

Return:

HtmlElement | DocumentFragment

If the parent level is above top level, it will returns a DocumentFragment

public parents(selector: String): Collection | HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source

Element's parent finder based on a DOM query selector

Params:

NameTypeAttributeDescription
selector String

A valid DOM query selector

Return:

Collection | HtmlElement | DocumentFragment

HtmlElement or DocumentFragment if no parent matches the selector

public prepend(e: String | Node | HtmlElement): HtmlElement version 1.0.0 since 1.0.0 source

Prepend an element to another

Params:

NameTypeAttributeDescription
e String | Node | HtmlElement

Element to prepend (enhanced or not)

Return:

HtmlElement

Return self for chaining

public previousSibling(n: Qtype): HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source

Element's previous sibling finder

Params:

NameTypeAttributeDescription
n Qtype
  • optional
  • default: 1

Distance to current Element

Return:

HtmlElement | DocumentFragment

If the sibling is unreachable, it will returns a DocumentFragment

public previousSiblings(selector: String): Collection | HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source

Element's previous siblings finder based on a DOM query selector

Params:

NameTypeAttributeDescription
selector String

A valid DOM query selector

Return:

Collection | HtmlElement | DocumentFragment

DocumentFragment if no siblings matches the selector

public remove(): HtmlElement version 1.0.0 since 1.0.0 source

Remove an Element

Return:

HtmlElement

Return for chaining

public removeClass(cls: String): HtmlElement version 1.0.0 since 1.0.0 source

Remove class(es) from an HtmlElement

Params:

NameTypeAttributeDescription
cls String

Class(es) name(s)

Return:

HtmlElement

Self for chaining

public shallow(): HtmlElement version 1.0.0 since 1.0.0 source

Returns a shallow copy of the HtmlElement

Return:

HtmlElement

Shallow copy of the current HtmlElement

public show(v: String): HtmlElement version 1.0.0 since 1.0.0 source

Show an HtmlElement by setting its display property. The property value can be override for custom results (e.g. 'flex')

Params:

NameTypeAttributeDescription
v String
  • optional
  • default: 'block'

Value for display property

Return:

HtmlElement

Self for chaining

public style(a: String | Array | Object | ObjectArray, v: String): HtmlElement | String | Object version 1.0.0 since 1.0.0 source

Get/Set style properties of an HtmlElement based on its node style attribute.

Properties name will be camelized when read from style string and dashed when set to style string.

Many syntaxes are available. See examples.

You can remove a style property by setting its value to null

Params:

NameTypeAttributeDescription
a String | Array | Object | ObjectArray
  • optional

get/set request on style attribute

  • Empty: style will returns all styles set in the stye attribute
  • String : Name or the style property to get or set if second parameter is provided
  • Array: Array of properties names to get
  • Object|ObjectArray : Properties names and values to set. If second parameter is set to true, it will replace all styles properties defined in the style attribute. Otherwise it will merge them
v String
  • optional

Value of the style or enable replace mode

Return:

HtmlElement | String | Object

Returns self for chaining for set cases, String or Object {styleKey: styleValue, ...} for get cases

Example:

 var e = new Element('<div style="color:red;margin:1em"></div>');
 e.style('color'); // returns 'red'
 e.style('color', 'yellow'); // Set color to yellow and returns e for chaining
 e.style(['color', 'margin']); // returns {color:'red', margin: '1em'}
 e.style({color: 'yellow', padding: '5px'}); // Set color to yellow, padding to 5px and returns e for chaining
 e.style({paddingLeft: '10px', margin: '5px'}); // Set color to yellow, padding to 5px and returns e for chaining

public toggle(v: String): HtmlElement version 1.0.0 since 1.0.0 source

Toggle an HtmlElement visibility by setting its display property. The "show" property value can be override for custom results (e.g. 'flex')

Use with care : It can be painfully slow and must be avoided for huge set of automated ops on elements if display property is not set inline

Params:

NameTypeAttributeDescription
v String
  • optional
  • default: 'block'

Value for display property when set to visible

Return:

HtmlElement

Self for chaining

public toggleClass(cls: String): HtmlElement version 1.0.0 since 1.0.0 source

Toggle class(es) from an HtmlElement

Params:

NameTypeAttributeDescription
cls String

Class(es) name(s)

Return:

HtmlElement

Self for chaining

public unwrap(): HtmlElement version 1.0.0 since 1.0.0 source

Unwrap an Element (and its siblings) and delete its parentNode

Return:

HtmlElement

Return for chaining

public wrap(e: String | Node | HtmlElement, returnself: Boolean): HtmlElement version 1.0.0 since 1.0.0 source

Wrap an Element into another. If no Element provided, the wrapper will be set to a DocumentFragment node.

The second parameter can be used to get the parent element for chaining instead of the element itself.

Params:

NameTypeAttributeDescription
e String | Node | HtmlElement
  • default: null

Wrapping Element

returnself Boolean
  • optional
  • default: true

If true, it will return the Element. If false, it will return the parent Element.

Return:

HtmlElement

Return for chaining

Private Methods

private _metricsUpdate(): void version 1.0.0 since 1.0.0 source

Refresh position object at first call or after a resize / changeOrientation event

Return:

void

private _parseDims(v: *): * source

Params:

NameTypeAttributeDescription
v *

Return:

*

private _scrollUpdate() source