RGJS6 DOM module.
Methods
-
<static> appendChild(element, parent)
-
Append an element to a parent.
Parameters:
Name Type Description element
element The element to append.
parent
element The parent.
Returns:
- Type
- element
-
<static> contains(lookup [, haystack])
-
Check if an alement contains an element.
The contains() method returns a Boolean value indicating whether a node is a descendant of a specified node.
A descendant can be a child, grandchild, great-grandchild, and so on.Parameters:
Name Type Argument Default Description lookup
element The element you're searching for.
haystack
element | document <optional>
document The element you're searching in. Defaults to document (or document.body).
Returns:
True or false.
- Type
- boolean
Example
rgjs.dom.contains(el); // True if el is in document (or document.body) rgjs.dom.contains(input_element, form_element) // True if the input_element is in the form_element.
-
<static> filterNodes(element, allowed)
-
Filter nodes and attributes based on their (tag)name.
Based on https://stackoverflow.com/a/2393182/781094Parameters:
Name Type Description element
element A html node/element. Can handle string, Node, NodeList and others.
allowed
object The tags and attributes that are allowed. Example: {link:['href', 'rel', type']}.
Returns:
The element, filtered.
- Type
- element
Example
// Filter everything except <p>, <ul>, <li> rgjs.dom.filterNodes(element, {p:[], ul:[], li:[]});
-
<static> getNodeIndex(element)
-
Get a node's index in its parent.
Based on https://stackoverflow.com/a/23107121/781094.Parameters:
Name Type Description element
element The node.
Returns:
The index of the node.
- Type
- number
Example
rgjs.dom.getNodeIndex(element) // `4` if it's the 4th child of its parent
-
<static> htmlAsElement(html [, elementType])
-
Wrap html in an element. Can handle other types (Node, NodeList, etc).
Parameters:
Name Type Argument Default Description html
string The html.
elementType
string <optional>
'div' Optional. The type of element to wrap the html in. Defaults to 'div'.
Returns:
An element with html's contents.
- Type
- element
Example
rgjs.dom.htmlAsElement('<div></div>') // returns an element with innerHTML `<div></div>`
-
<static> insertAfter(element, target)
-
Insert an element after a target.
Parameters:
Name Type Description element
element The element to insert.
target
element The target.
Returns:
- Type
- element
-
<static> insertBefore(element, target)
-
Insert an element before a target.
Parameters:
Name Type Description element
element The element to insert.
target
element The target.
Returns:
- Type
- element
-
<static> isVisible(element)
-
Check if an element is visible. Based on jQuery :visible
Parameters:
Name Type Description element
element The element.
Returns:
True if the element is in the DOM and offsetWidth, offsetHeight > 0.
- Type
- boolean
-
<static> prependChild(element, parent)
-
Prepend an element to a parent.
Parameters:
Name Type Description element
element The element to append.
parent
element The parent.
Returns:
- Type
- element
-
<static> removeElement(element)
-
Remove an element.
Parameters:
Name Type Description element
element The element to append.
Returns:
- Type
- boolean
-
<static> selectElements(selector [, _parent])
-
Get elements based on a selector.
Parameters:
Name Type Argument Default Description selector
* The selector for the element. CSS selector, NodeList, HTMLCollection, jQuery and Node are supported. You can also pass an array.
_parent
element <optional>
document Optional. The element to look for selector. Defaults to document.
Returns:
An array containing elements (or none).
- Type
- array
Examples
// Use a css selector rgjs.dom.selectElements('div')
// Use a Node rgjs.dom.selectElements(document.querySelector('div'))
// Use a NodeList rgjs.dom.selectElements(document.querySelectorAll('div'))
// Use a jQuery object rgjs.dom.selectElements($('div'))
// Use a combination of the above rgjs.dom.selectElements(['div', document.querySelectorAll('div'), etc])