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 element
HTMLElement | SVGElement target element
cssClass
string <repeatable>
css classes to add
-
static checkMouse(element, e) → {boolean}
-
Check mouse was leave event element with ignoreing child nodes
Parameters:
Name Type Description element
HTMLElement element to check
e
MouseEvent mouse event
Returns:
boolean -whether mouse leave element?
-
static closest(element, selector) → {HTMLElement}
-
Find parent element recursively
Parameters:
Name Type Description element
HTMLElement base element to start find
selector
string selector string for find
Returns:
HTMLElement -- element finded or null
-
static css(element, key, valueopt)
-
Setting element style
Parameters:
Name Type Attributes Description element
HTMLElement | SVGElement element to setting style
key
string | object style prop name or {prop: value} pair object
value
string <optional>
style value
-
static disableTextSelection(elopt)
-
Disable browser's text selection behaviors.
Parameters:
Name Type Attributes Description el
HTMLElement <optional>
target element. if not supplied, use
document
-
static enableTextSelection(elopt)
-
Enable browser's text selection behaviors.
Parameters:
Name Type Attributes Description el
HTMLElement <optional>
target element. if not supplied, use
document
-
static find(elementopt, selectoropt) → {HTMLElement}
-
Find single element
Parameters:
Name Type Attributes Default Description element
HTMLElement | string <optional>
document base element to find
selector
string <optional>
css selector
Returns:
HTMLElement -
static findAll(elementopt, selectoropt) → {Array.<HTMLElement>}
-
Find multiple element
Parameters:
Name Type Attributes Default Description element
HTMLElement | string <optional>
document base element to find
selector
string <optional>
css selector
Returns:
Array.<HTMLElement> -
static getClass(element) → {string}
-
Get HTML element's design classes.
Parameters:
Name Type Description element
HTMLElement | SVGElement target element
Returns:
string -element css class name
-
static getData(element, key) → {string}
-
Get data value from data-attribute
Parameters:
Name Type Description element
HTMLElement target element
key
string 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 mouseEvent
MouseEvent 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 position
MouseEvent | object | Array.<number> mouse position object
relativeElement
HTMLElement HTML element that calculate relative position
Returns:
Array.<number> -mouse position
-
static getRect(element) → {object}
-
getBoundingClientRect polyfill
Parameters:
Name Type Description element
HTMLElement target element
Returns:
object -rect object
-
static hasClass(element, cssClass) → {boolean}
-
Check element has specific css class
Parameters:
Name Type Description element
HTMLElement | SVGElement target element
cssClass
string css class
Returns:
boolean -
static insertAfter(element, target)
-
Insert element to next of target element
Parameters:
Name Type Description element
HTMLElement html element to insert
target
HTMLElement target element
-
static matches(element, selector) → {boolean}
-
Check element match selector
Parameters:
Name Type Description element
HTMLElement element to check
selector
string selector to check
Returns:
boolean -is selector matched to element?
-
static off(element, types, handler)
-
Unbind DOM events
Parameters:
Name Type Description element
HTMLElement element to unbindbind events
types
string | object Space splitted events names or eventName:handler object
handler
function | object handler function or context for handler method
-
static on(element, types, handler, contextopt)
-
Bind DOM events
Parameters:
Name Type Attributes Description element
HTMLElement element to bind events
types
string | object Space splitted events names or eventName:handler object
handler
function | object handler function or context for handler method
context
object <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 element
HTMLElement HTMLElement to bind events.
types
string | 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 e
Event event object
-
static removeClass(element, …cssClass)
-
Remove css class from element
Parameters:
Name Type Attributes Description element
HTMLElement | SVGElement target element
cssClass
string <repeatable>
css classes to remove
-
static removeData(element, key)
-
Remove data property
Parameters:
Name Type Description element
HTMLElement target element
key
string key
-
static removeElement(element)
-
Remove element from parent node.
Parameters:
Name Type Description element
HTMLElement element to remove.
-
static setBound(element, bound)
-
Set element bound
Parameters:
Name Type Description element
HTMLElement element to change bound
bound
object bound object
Properties
Name Type Attributes Description top
number <optional>
top pixel
right
number <optional>
right pixel
bottom
number <optional>
bottom pixel
left
number <optional>
left pixel
width
number <optional>
width pixel
height
number <optional>
height pixel
-
static setData(element, key, value)
-
Set data attribute to target element
Parameters:
Name Type Description element
HTMLElement element to set data attribute
key
string key
value
string value
-
static stopPropagation(e)
-
Stop event propagation.
Parameters:
Name Type Description e
Event event object
-
static testCSSProp(props) → {string|boolean}
-
Check specific CSS style is available.
Parameters:
Name Type Description props
array 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 element
HTMLElement html element
Returns:
string -text content
-
static toggleClass(element, …cssClass)
-
Toggle css class
Parameters:
Name Type Attributes Description element
HTMLElement | SVGElement target element
cssClass
string <repeatable>
css classes to toggle
-
static upperToHyphenLower(match) → {string}
-
Convert uppercase letter to hyphen lowercase character
Parameters:
Name Type Description match
string match from String.prototype.replace method
Returns:
string