DOM manipulation utilities
Examples
// node, commonjs
var domUtil = require('tui-dom');
domUtil.addClass(element, 'foo', 'bar');
// distribution file, script
<script src='path/to/tui-dom.js'></script>
<script>
var domUtil = tui.dom;
domUtil.addClass(element, 'foo', 'bar');
</script>
Methods
-
static addClass(element, …cssClass)
-
Add css class to element
Parameters:
Name Type Attributes Description elementHTMLElement | SVGElement target element
cssClassstring <repeatable>
css classes to add
-
static checkMouse(element, e) → {boolean}
-
Check mouse was leave event element with ignoreing child nodes
Parameters:
Name Type Description elementHTMLElement element to check
eMouseEvent mouse event
Returns:
boolean -whether mouse leave element?
-
static closest(element, selector) → {HTMLElement}
-
Find parent element recursively
Parameters:
Name Type Description elementHTMLElement base element to start find
selectorstring selector string for find
Returns:
HTMLElement -- element finded or null
-
static css(element, key, valueopt)
-
Setting element style
Parameters:
Name Type Attributes Description elementHTMLElement | SVGElement element to setting style
keystring | object style prop name or {prop: value} pair object
valuestring <optional>
style value
-
static disableTextSelection(elopt)
-
Disable browser's text selection behaviors.
Parameters:
Name Type Attributes Description elHTMLElement <optional>
target element. if not supplied, use
document -
static enableTextSelection(elopt)
-
Enable browser's text selection behaviors.
Parameters:
Name Type Attributes Description elHTMLElement <optional>
target element. if not supplied, use
document -
static find(elementopt, selectoropt) → {HTMLElement}
-
Find single element
Parameters:
Name Type Attributes Default Description elementHTMLElement | string <optional>
document base element to find
selectorstring <optional>
css selector
Returns:
HTMLElement -
static findAll(elementopt, selectoropt) → {Array.<HTMLElement>}
-
Find multiple element
Parameters:
Name Type Attributes Default Description elementHTMLElement | string <optional>
document base element to find
selectorstring <optional>
css selector
Returns:
Array.<HTMLElement> -
static getClass(element) → {string}
-
Get HTML element's design classes.
Parameters:
Name Type Description elementHTMLElement | SVGElement target element
Returns:
string -element css class name
-
static getData(element, key) → {string}
-
Get data value from data-attribute
Parameters:
Name Type Description elementHTMLElement target element
keystring key
Returns:
string -value
-
static getMouseButton(mouseEvent) → {number}
-
Normalize mouse event's button attributes.
Can detect which button is clicked by this method.
Meaning of return numbers
- 0: primary mouse button
- 1: wheel button or center button
- 2: secondary mouse button
Parameters:
Name Type Description mouseEventMouseEvent The mouse event object want to know.
Returns:
number -- The value of meaning which button is clicked?
-
static getMousePosition(position, relativeElement) → {Array.<number>}
-
Get mouse position from mouse event
If supplied relatveElement parameter then return relative position based on element
Parameters:
Name Type Description positionMouseEvent | object | Array.<number> mouse position object
relativeElementHTMLElement HTML element that calculate relative position
Returns:
Array.<number> -mouse position
-
static getRect(element) → {object}
-
getBoundingClientRect polyfill
Parameters:
Name Type Description elementHTMLElement target element
Returns:
object -rect object
-
static hasClass(element, cssClass) → {boolean}
-
Check element has specific css class
Parameters:
Name Type Description elementHTMLElement | SVGElement target element
cssClassstring css class
Returns:
boolean -
static insertAfter(element, target)
-
Insert element to next of target element
Parameters:
Name Type Description elementHTMLElement html element to insert
targetHTMLElement target element
-
static matches(element, selector) → {boolean}
-
Check element match selector
Parameters:
Name Type Description elementHTMLElement element to check
selectorstring selector to check
Returns:
boolean -is selector matched to element?
-
static off(element, types, handler)
-
Unbind DOM events
Parameters:
Name Type Description elementHTMLElement element to unbindbind events
typesstring | object Space splitted events names or eventName:handler object
handlerfunction | object handler function or context for handler method
-
static on(element, types, handler, contextopt)
-
Bind DOM events
Parameters:
Name Type Attributes Description elementHTMLElement element to bind events
typesstring | object Space splitted events names or eventName:handler object
handlerfunction | object handler function or context for handler method
contextobject <optional>
context - context for handler method.
-
static once(element, types, handler, contextopt)
-
Bind DOM event. this event will unbind after invokes.
Parameters:
Name Type Attributes Description elementHTMLElement HTMLElement to bind events.
typesstring | object Space splitted events names or eventName:handler object.
handler* handler function or context for handler method.
context* <optional>
context object for handler method.
-
static preventDefault(e)
-
Prevent default action
Parameters:
Name Type Description eEvent event object
-
static removeClass(element, …cssClass)
-
Remove css class from element
Parameters:
Name Type Attributes Description elementHTMLElement | SVGElement target element
cssClassstring <repeatable>
css classes to remove
-
static removeData(element, key)
-
Remove data property
Parameters:
Name Type Description elementHTMLElement target element
keystring key
-
static removeElement(element)
-
Remove element from parent node.
Parameters:
Name Type Description elementHTMLElement element to remove.
-
static setBound(element, bound)
-
Set element bound
Parameters:
Name Type Description elementHTMLElement element to change bound
boundobject bound object
Properties
Name Type Attributes Description topnumber <optional>
top pixel
rightnumber <optional>
right pixel
bottomnumber <optional>
bottom pixel
leftnumber <optional>
left pixel
widthnumber <optional>
width pixel
heightnumber <optional>
height pixel
-
static setData(element, key, value)
-
Set data attribute to target element
Parameters:
Name Type Description elementHTMLElement element to set data attribute
keystring key
valuestring value
-
static stopPropagation(e)
-
Stop event propagation.
Parameters:
Name Type Description eEvent event object
-
static testCSSProp(props) → {string|boolean}
-
Check specific CSS style is available.
Parameters:
Name Type Description propsarray property name to testing
Returns:
string | boolean -return true when property is available
Example
//-- #1. Get Module --// var domUtil = require('tui-dom'); // node, commonjs var domUtil = tui.dom; // distribution file //-- #2. Use property --// var props = ['transform', '-webkit-transform']; domutil.testCSSProp(props); // 'transform' -
static textContent(element) → {string}
-
Represents the text content of a node and its descendants
Parameters:
Name Type Description elementHTMLElement html element
Returns:
string -text content
-
static toggleClass(element, …cssClass)
-
Toggle css class
Parameters:
Name Type Attributes Description elementHTMLElement | SVGElement target element
cssClassstring <repeatable>
css classes to toggle
-
static upperToHyphenLower(match) → {string}
-
Convert uppercase letter to hyphen lowercase character
Parameters:
Name Type Description matchstring match from String.prototype.replace method
Returns:
string