Methods
-
Appends the given children data to the given node.
Name Type Description node* The
Nodeargument to append the children to.children* optional The children to append to the given node.
When
childrenis an array, then each item of the array will be either appended as a child element or text node, depending on whether the item is a DOMNodeinstance or some other non-nullvalue. Non-Node, non-nullvalues will be converted to strings first before being passed as argument tocreateTextNode().When
childrenis a function, it will be invoked with the passednodeargument as the sole parameter and theappendfunction will be invoked again, with the givennodeargument as first and the return value of thechildrenfunction as the second parameter.When
childrenis a DOMNodeinstance, it will be appended to the givennode.When
childrenis any other non-nullvalue, it will be converted to a string and appended to theinnerHTMLproperty of the givennode.Returns:
Type Description Node | null Returns the last children Nodeappended to the node ornullif either thenodeargument was no valid DOMnodeor if thechildrenwasnullor didn't result in further DOM nodes. -
Sets attributes or registers event listeners on element nodes.
Name Type Description node* The
Nodeargument to set the attributes or add the event listeners for. When the givennodevalue is not a valid DOMNode, the function returns and does nothing.keystring | Object.<string, *> Specifies either the attribute or event handler name to use, or an object containing multiple key, value pairs which are each added to the node as either attribute or event handler, depending on the respective value.
val* optional Specifies the attribute value or event handler function to add. If the
keyparameter is anObject, this parameter will be ignored.When
valis of type function, it will be registered as an event handler on the givennodewith thekeyparameter being the event name.When
valis of type object, it will be serialized as JSON and added as an attribute to the givennode, using the givenkeyas an attribute name.When
valis of any other type, it will be added as an attribute to the givennodeas-is, with the underlyingsetAttribute()call implicitly turning it into a string. -
Binds the given class instance to the specified DOM
Node.This function uses the
dom.data()facility to attach the passed instance of a Class to a node. This is needed for complex widget elements or similar where the corresponding class instance responsible for the element must be retrieved from DOM nodes obtained byquerySelector()or similar means.Name Type Description nodeNode The DOM
Nodeinstance to bind the class to.instClass The Class instance to bind to the node.
Throws:
-
Throws a
TypeErrorwhen the given instance argument isn't a valid Class instance. - Type
- TypeError
Returns:
Type Description Class Returns the bound class instance. -
-
Finds a bound class instance on the given node itself or the first bound instance on its closest parent node and invokes the specified method name on the found class instance.
Name Type Description nodeNode The DOM
Nodeinstance to start from.methodstring The name of the method to invoke on the found class instance.
params* repeatable Additional arguments to pass to the invoked method as-is.
Returns:
Type Description * | null Returns the return value of the invoked method if a class instance and method has been found. Returns nullif either no bound class instance could be found, or if the found instance didn't have the requestedmethod. -
Replaces the content of the given node with the given children.
This function first removes any children of the given DOM
Nodeand then adds the given children following the rules outlined below.Name Type Description node* The
Nodeargument to replace the children of.children* optional The children to replace into the given node.
When
childrenis an array, then each item of the array will be either appended as a child element or text node, depending on whether the item is a DOMNodeinstance or some other non-nullvalue. Non-Node, non-nullvalues will be converted to strings first before being passed as argument tocreateTextNode().When
childrenis a function, it will be invoked with the passednodeargument as the sole parameter and theappendfunction will be invoked again, with the givennodeargument as first and the return value of thechildrenfunction as the second parameter.When
childrenis a DOMNodeinstance, it will be appended to the givennode.When
childrenis any other non-nullvalue, it will be converted to a string and appended to theinnerHTMLproperty of the givennode.Returns:
Type Description Node | null Returns the last children Nodeappended to the node ornullif either thenodeargument was no valid DOMnodeor if thechildrenwasnullor didn't result in further DOM nodes. -
Creates a new DOM
Nodefrom the givenhtml,attranddataparameters.This function has multiple signatures, it can be either invoked in the form
create(html[, attr[, data]])or in the formcreate(html[, data]). The used variant is determined from the type of the second argument.Name Type Description html* Describes the node to create.
When the value of
htmlis of type array, aDocumentFragmentnode is created and each item of the array is first converted to a DOMNodeby passing it throughcreate()and then added as a child to the fragment.When the value of
htmlis a DOMNodeinstance, no new element will be created, but the node will be used as-is.When the value of
htmlis a string starting with<, it will be passed todom.parse()and the resulting value is used.When the value of
htmlis any other string, it will be passed todocument.createElement()for creating a new DOMNodeof the given name.attrObject.<string, *> optional Specifies an Object of key, value pairs to set as attributes or event handlers on the created node. Refer to
dom.attr()for details.data* optional Specifies children to append to the newly created element. Refer to
dom.append()for details.Throws:
-
Throws an
InvalidCharacterErrorwhen the givenhtmlargument contained malformed markup (such as not escaped&characters in XHTML mode) or when the given node name inhtmlcontains characters which are not legal in DOM element names, such as spaces. - Type
- InvalidCharacterError
Returns:
Type Description Node Returns the newly created Node. -
-
Attaches or detaches arbitrary data to and from a DOM
Node.This function is useful to attach non-string values or runtime data that is not serializable to DOM nodes. To decouple data from the DOM, values are not added directly to nodes, but inserted into a registry instead which is then referenced by a string key stored as
data-idrefattribute in the node.This function has multiple signatures and is sensitive to the number of arguments passed to it.
dom.data(node)- Fetches all data associated with the given node.dom.data(node, key)- Fetches a specific key associated with the given node.dom.data(node, key, val)- Sets a specific key to the given value associated with the given node.dom.data(node, null)- Clears any data associated with the node.dom.data(node, key, null)- Clears the given key associated with the node.
Name Type Description nodeNode The DOM
Nodeinstance to set or retrieve the data for.keystring | null optional This is either a string specifying the key to retrieve, or
nullto unset the entire node data.val* | null optional This is either a non-
nullvalue to set for a given key ornullto remove the givenkeyfrom the specified node.Returns:
Type Description * Returns the get or set value, or nullwhen no value could be found. -
Tests whether the given argument is a valid DOM
Node.Name Type Description e* The value to test.
Returns:
Type Description boolean Returns trueif the value is a DOMNode, elsefalse. -
Finds a bound class instance on the given node itself or the first bound instance on its closest parent node.
Name Type Description nodeNode The DOM
Nodeinstance to start from.Returns:
Type Description Class | null Returns the founds class instance if any or nullif no bound class could be found on the node itself or any of its parents. -
Tests whether a given DOM
Nodeinstance is empty or appears empty.Any element child nodes which have the CSS class
hiddenset or for which the optionally passedignoreFncallback function returnsfalseare ignored.Name Type Description nodeNode The DOM
Nodeinstance to test.ignoreFnLuCI.dom~ignoreCallbackFn optional Specifies an optional function which is invoked for each child node to decide whether the child node should be ignored or not.
Returns:
Type Description boolean Returns trueif the node does not have any children or if any children node either has ahiddenCSS class or afalseresult when testing it using the givenignoreFn. -
Tests whether a given
Nodematches the given query selector.This function is a convenience wrapper around the standard
Node.matches("selector")function with the added benefit that thenodeargument may be a non-Nodevalue, in which case this function simply returnsfalse.Name Type Description node* The
Nodeargument to test the selector against.selectorstring optional The query selector expression to test against the given node.
Returns:
Type Description boolean Returns trueif the given node matches the specified selector orfalsewhen the node argument is no valid DOMNodeor the selector didn't match. -
Returns the closest parent node that matches the given query selector expression.
This function is a convenience wrapper around the standard
Node.closest("selector")function with the added benefit that thenodeargument may be a non-Nodevalue, in which case this function simply returnsnull.Name Type Description node* The
Nodeargument to find the closest parent for.selectorstring optional The query selector expression to test against each parent.
Returns:
Type Description Node | null Returns the closest parent node matching the selector or nullwhen the node argument is no valid DOMNodeor the selector didn't match any parent. -
Parses a given string as HTML and returns the first child node.
Name Type Description sstring A string containing an HTML fragment to parse. Note that only the first result of the resulting structure is returned, so an input value of
<div>foo</div> <div>bar</div>will only return the firstdivelement node.Returns:
Type Description Node Returns the first DOM Nodeextracted from the HTML fragment ornullon parsing failures or if no element could be found.
Type Definitions
-
The ignore callback function is invoked by
isEmpty()for each child node to decide whether to ignore a child node or not.When this function returns
false, the node passed to it is ignored, else not.Name Type Description nodeNode The child node to test.
Returns:
Type Description boolean Boolean indicating whether to ignore the node or not.