DOM Class
Provides DOM helper methods.
Item Index
Methods
- _bruteContains
- _getDoc
- _getRegExp
- _getWin
- addClass
- addHTML
- byId
- contains
- create
- docHeight
- docScrollX
- docScrollY
- docWidth
- elementByAxis
- getAttribute
- getComputedStyle
- getScrollbarWidth
- getStyle
- getText
- getX
- getXY
- getY
- hasClass
- inDoc
- inRegion
- intersect
- inViewportRegion
- region
- removeClass
- replaceClass
- setAttribute
- setHeight
- setStyle
- setStyles
- setText
- setWidth
- setX
- setXY
- setY
- swapXY
- toggleClass
- viewportRegion
- winHeight
- winWidth
Methods
_bruteContains
-
element
-
needle
Brute force version of contains. Used for browsers without contains support for non-HTMLElement Nodes (textNodes, etc).
Parameters:
-
element
HTMLElementThe containing html element.
-
needle
HTMLElementThe html element that may be contained.
Returns:
Whether or not the element is or contains the needle.
_getDoc
-
element
returns the appropriate document.
Parameters:
-
element
HTMLElementoptional Target element.
Returns:
The document for the given element or the default document.
_getRegExp
-
str
-
flags
Memoizes dynamic regular expressions to boost runtime performance.
Parameters:
Returns:
An instance of RegExp
_getWin
-
element
returns the appropriate window.
Parameters:
-
element
HTMLElementoptional Target element.
Returns:
The window for the given element or the default window.
addClass
-
element
-
className
Adds a class name to a given DOM element.
Parameters:
-
element
HTMLElementThe DOM element.
-
className
Stringthe class name to add to the class attribute
addHTML
-
node
-
content
-
where
Inserts content in a node at the given location
Parameters:
-
node
HTMLElementThe node to insert into
-
content
HTMLElement | Array | HTMLCollectionThe content to be inserted
-
where
HTMLElementWhere to insert the content If no "where" is given, content is appended to the node Possible values for "where"
- HTMLElement
- The element to insert before
- "replace"
- Replaces the existing HTML
- "before"
- Inserts before the existing HTML
- "before"
- Inserts content before the node
- "after"
- Inserts content after the node
byId
-
id
-
doc
Returns the HTMLElement with the given ID (Wrapper for document.getElementById).
Parameters:
Returns:
The HTMLElement with the id, or null if none found.
contains
-
element
-
needle
Determines whether or not one HTMLElement is or contains another HTMLElement.
Parameters:
-
element
HTMLElementThe containing html element.
-
needle
HTMLElementThe html element that may be contained.
Returns:
Whether or not the element is or contains the needle.
create
-
html
-
doc
Creates a new dom node using the provided markup string.
Parameters:
-
html
StringThe markup used to create the element
-
doc
HTMLDocumentAn optional document context
Returns:
returns a single HTMLElement when creating one node, and a documentFragment when creating multiple nodes.
docScrollX
()
Number
Amount page has been scroll horizontally
Returns:
The current amount the screen is scrolled horizontally.
docScrollY
()
Number
Amount page has been scroll vertically
Returns:
The current amount the screen is scrolled vertically.
elementByAxis
-
element
-
axis
-
[fn]
-
[all]
Searches the element by the given axis for the first matching element.
Parameters:
-
element
HTMLElementThe html element.
-
axis
StringThe axis to search (parentNode, nextSibling, previousSibling).
-
[fn]
Function optionalAn optional boolean test to apply.
-
[all]
Boolean optionalWhether text nodes as well as element nodes should be returned, or just element nodes will be returned(default) The optional function is passed the current HTMLElement being tested as its only argument. If no function is given, the first element is returned.
Returns:
The matching element or null if none found.
getAttribute
-
el
-
attr
Provides a normalized attribute interface.
Parameters:
-
el
HTMLElementThe target element for the attribute.
-
attr
StringThe attribute to get.
Returns:
The current value of the attribute.
getComputedStyle
-
node
-
att
Returns the computed style for the given node.
Parameters:
-
node
HTMLElementThe HTMLElement to get the style from.
-
att
StringThe style property to get.
Returns:
The computed value of the style property.
getScrollbarWidth
()
Number
Gets the width of vertical scrollbars on overflowed containers in the body content.
Returns:
Pixel width of a scrollbar in the current browser
getStyle
-
node
-
att
-
[style]
Returns the current style value for the given property.
Parameters:
-
node
HTMLElementThe HTMLElement to get the style from.
-
att
StringThe style property to get.
-
[style]
Object optionalThe style node. Defaults to
node.style
.
getText
-
element
Returns the text content of the HTMLElement.
Parameters:
-
element
HTMLElementThe html element.
Returns:
The text content of the element (includes text of any descending elements).
getX
-
element
Gets the current X position of an element based on page coordinates. Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
Parameters:
-
element
ObjectThe target element
Returns:
The X position of the element
getXY
-
element
Gets the current position of an element based on page coordinates. Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
Parameters:
-
element
ObjectThe target element
Returns:
getY
-
element
Gets the current Y position of an element based on page coordinates. Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
Parameters:
-
element
ObjectThe target element
Returns:
The Y position of the element
hasClass
-
element
-
className
Determines whether a DOM element has the given className.
Parameters:
-
element
HTMLElementThe DOM element.
-
className
Stringthe class name to search for
Returns:
Whether or not the element has the given class.
inDoc
-
element
-
doc
Determines whether or not the HTMLElement is part of the document.
Parameters:
-
element
HTMLElementThe containing html element.
-
doc
HTMLElementoptional The document to check.
Returns:
Whether or not the element is attached to the document.
inRegion
-
node
-
node2
-
all
-
altRegion
Check if any part of this node is in the passed region
Parameters:
-
node
ObjectThe node to get the region from
-
node2
ObjectThe second node to get the region from or an Object literal of the region
-
all
BooleanShould all of the node be inside the region
-
altRegion
ObjectAn object literal containing the region for this node if we already have the data (for performance e.g. DragDrop)
Returns:
True if in region, false if not.
intersect
-
element
-
element2
-
altRegion
Find the intersect information for the passed nodes.
Parameters:
-
element
HTMLElementThe first element
-
element2
HTMLElement | ObjectThe element or region to check the interect with
-
altRegion
ObjectAn object literal containing the region for the first element if we already have the data (for performance e.g. DragDrop)
Returns:
Object literal containing the following intersection data: (top, right, bottom, left, area, yoff, xoff, inRegion)
inViewportRegion
-
element
-
all
-
altRegion
Check if any part of this element is in the viewport
Parameters:
-
element
HTMLElementThe DOM element.
-
all
BooleanShould all of the node be inside the region
-
altRegion
ObjectAn object literal containing the region for this node if we already have the data (for performance e.g. DragDrop)
Returns:
True if in region, false if not.
region
-
element
Returns an Object literal containing the following about this element: (top, right, bottom, left)
Parameters:
-
element
HTMLElementThe DOM element.
Returns:
Object literal containing the following about this element: (top, right, bottom, left)
removeClass
-
element
-
className
Removes a class name from a given element.
Parameters:
-
element
HTMLElementThe DOM element.
-
className
Stringthe class name to remove from the class attribute
replaceClass
-
element
-
oldClassName
-
newClassName
Replace a class with another class for a given element. If no oldClassName is present, the newClassName is simply added.
Parameters:
-
element
HTMLElementThe DOM element
-
oldClassName
Stringthe class name to be replaced
-
newClassName
Stringthe class name that will be replacing the old class name
setAttribute
-
el
-
attr
-
val
Provides a normalized attribute interface.
Parameters:
-
el
HTMLElementThe target element for the attribute.
-
attr
StringThe attribute to set.
-
val
StringThe value of the attribute.
setHeight
-
element
-
size
Sets the height of the element to the given size, regardless of box model, border, padding, etc.
Parameters:
-
element
HTMLElementThe DOM element.
-
size
String | NumberThe pixel height to size to
setStyle
-
node
-
att
-
val
-
[style]
Sets a style property for a given element.
Parameters:
-
node
HTMLElementThe HTMLElement to apply the style to.
-
att
StringThe style property to set.
-
val
String | NumberThe value.
-
[style]
Object optionalThe style node. Defaults to
node.style
.
setStyles
-
node
-
hash
Sets multiple style properties.
Parameters:
-
node
HTMLElementThe HTMLElement to apply the styles to.
-
hash
ObjectAn object literal of property:value pairs.
setText
-
element
-
content
Sets the text content of the HTMLElement.
Parameters:
-
element
HTMLElementThe html element.
-
content
StringThe content to add.
setWidth
-
element
-
size
Sets the width of the element to the given size, regardless of box model, border, padding, etc.
Parameters:
-
element
HTMLElementThe DOM element.
-
size
String | NumberThe pixel height to size to
setX
-
element
-
x
Set the X position of an html element in page coordinates, regardless of how the element is positioned. The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
setXY
-
element
-
xy
-
noRetry
Set the position of an html element in page coordinates. The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
setY
-
element
-
y
Set the Y position of an html element in page coordinates, regardless of how the element is positioned. The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
toggleClass
-
element
-
className
-
addClass
If the className exists on the node it is removed, if it doesn't exist it is added.
Parameters:
-
element
HTMLElementThe DOM element
-
className
Stringthe class name to be toggled
-
addClass
Booleanoptional boolean to indicate whether class should be added or removed regardless of current state
viewportRegion
()
Object
Returns an Object literal containing the following about the visible region of viewport: (top, right, bottom, left)
Returns:
Object literal containing the following about the visible region of the viewport: (top, right, bottom, left)