dom.js
Utilities for manipulating the browser's Document Object Model Inspiration taken *heavily* from mochikit (http://mochikit.com/). If you want to do extensive DOM building you can create local aliases, such as:
var $DIV = goog.bind(goog.dom.createDom, goog.dom, 'div');
var $A = goog.bind(goog.dom.createDom, goog.dom, 'a');
var $TABLE = goog.bind(goog.dom.createDom, goog.dom, 'table');
You can use {@link goog.dom.DomHelper} to create new dom helpers that refer to a different document object. This is useful if you are working with frames or multiple windows.

File Location

dom/dom.js

Classes

goog.dom.DomHelper
Create an instance of a DOM helper with a new document object.

Public Protected Private

Global Functions

goog.dom.$()
Alias for getElement.
Returns:   The element with the given ID, or the node passed in.
code »
goog.dom.$$()
{length: number}
Alias for {@code getElementsByTagNameAndClass}.
Returns: 
{length: number}
  Array-like list of elements (only a length property and numerical indices are guaranteed to exist).
code »
goog.dom.$dom()
Alias for {@code createDom}.
Returns:   Reference to a DOM node.
code »
goog.dom.appendChild(parentchild)
Appends a child to a node.
Arguments:
parent :
Parent.
child :
Child.
code »
goog.dom.canHaveChildren(node)
Determines if the given node can contain children.
Arguments:
node :
The node to check.
Returns:   Whether the node can contain children.
code »
goog.dom.compareNodeOrder(node1node2)
Compares the document order of two nodes, returning 0 if they are the same node, a negative number if node1 is before node2, and a positive number if node2 is before node1. Note that we compare the order the tags appear in the document so in the tree text the B node is considered to be before the I node.
Arguments:
node1 :
The first node to compare.
node2 :
The second node to compare.
Returns:   0 if the nodes are the same node, a negative number if node1 is before node2, and a positive number if node2 is before node1.
code »
goog.dom.compareParentsDescendantNodeIe_(textNodenode)
Utility function to compare the position of two nodes, when {@code textNode}'s parent is an ancestor of {@code node}. If this entry condition is not met, this function will attempt to reference a null object.
Arguments:
textNode :
The textNode to compare.
node :
The node to compare.
Returns:   -1 if node is before textNode, +1 otherwise.
code »
goog.dom.compareSiblingOrder_(node1node2)
Utility function to compare the position of two nodes known to be non-equal siblings.
Arguments:
node1 :
The first node to compare.
node2 :
The second node to compare.
Returns:   -1 if node1 is before node2, +1 otherwise.
code »
goog.dom.contains(parentdescendant)
Whether a node contains another node.
Arguments:
parent :
The node that should contain the other node.
descendant :
The node to test presence of.
Returns:   Whether the parent node contains the descendent node.
code »
goog.dom.createDom(tagNameopt_attributesvar_args)
Returns a dom node with a set of attributes. This function accepts varargs for subsequent nodes to be added. Subsequent nodes will be added to the first node as childNodes. So: createDom('div', null, createDom('p'), createDom('p')); would return a div with two child paragraphs
Arguments:
tagName :
Tag to create.
opt_attributes :
If object, then a map of name-value pairs for attributes. If a string, then this is the className of the new element.
var_args :
Object | string | Array | NodeList
Further DOM nodes or strings for text nodes. If one of the var_args is an array or NodeList, its elements will be added as childNodes instead.
Returns:   Reference to a DOM node.
code »
goog.dom.createDom_(docargs)
Helper for {@code createDom}.
Arguments:
doc :
The document to create the DOM in.
args :
Arguments
Argument object passed from the callers. See {@code goog.dom.createDom} for details.
Returns:   Reference to a DOM node.
code »
goog.dom.createElement(name)
Creates a new element.
Arguments:
name :
Tag name.
Returns:   The new element.
code »
goog.dom.createTextNode(content)
!Text
Creates a new text node.
Arguments:
content :
Content.
Returns: 
!Text
  The new text node.
code »
goog.dom.findCommonAncestor(var_args)
Find the deepest common ancestor of the given nodes.
Arguments:
var_args :
The nodes to find a common ancestor of.
Returns:   The common ancestor of the nodes, or null if there is none. null will only be returned if two or more of the nodes are from different documents.
code »
goog.dom.findNode(rootp)
Node | undefined
Finds the first descendant node that matches the filter function, using a depth first search. This function offers the most general purpose way of finding a matching element. You may also wish to consider {@code goog.dom.query} which can express many matching criteria using CSS selector expressions. These expressions often result in a more compact representation of the desired result.
Arguments:
root :
The root of the tree to search.
p :
function(Node) : boolean
The filter function.
Returns: 
Node | undefined
  The found node or undefined if none is found.
code »
goog.dom.findNodes(rootp)
Finds all the descendant nodes that match the filter function, using a a depth first search. This function offers the most general-purpose way of finding a set of matching elements. You may also wish to consider {@code goog.dom.query} which can express many matching criteria using CSS selector expressions. These expressions often result in a more compact representation of the desired result.
Arguments:
root :
The root of the tree to search.
p :
function(Node) : boolean
The filter function.
Returns:   The found nodes or an empty array if none are found.
code »
goog.dom.findNodes_(rootprvfindOne)
Finds the first or all the descendant nodes that match the filter function, using a depth first search.
Arguments:
root :
The root of the tree to search.
p :
function(Node) : boolean
The filter function.
rv :
The found nodes are added to this array.
findOne :
If true we exit after the first found node.
Returns:   Whether the search is complete or not. True in case findOne is true and the node is found. False otherwise.
code »
goog.dom.flattenElement(element)
Element | undefined
Flattens an element. That is, removes it and replace it with its children. Does nothing if the element is not in the document.
Arguments:
element :
The element to flatten.
Returns: 
Element | undefined
  The original element, detached from the document tree, sans children; or undefined, if the element was not in the document to begin with.
code »
goog.dom.getAncestor(elementmatcheropt_includeNodeopt_maxSearchSteps)
Walks up the DOM hierarchy returning the first ancestor that passes the matcher function.
Arguments:
element :
The DOM node to start with.
matcher :
function(Node) : boolean
A function that returns true if the passed node matches the desired criteria.
opt_includeNode :
If true, the node itself is included in the search (the first call to the matcher will pass startElement as the node to test).
opt_maxSearchSteps :
Maximum number of levels to search up the dom.
Returns:   DOM node that matched the matcher, or null if there was no match.
code »
goog.dom.getAncestorByTagNameAndClass(elementopt_tagopt_class)
Walks up the DOM hierarchy returning the first ancestor that has the passed tag name and/or class name. If the passed element matches the specified criteria, the element itself is returned.
Arguments:
element :
The DOM node to start with.
opt_tag :
The tag name to match (or null/undefined to match any node regardless of tag name). Must be uppercase (goog.dom.TagName).
opt_class :
The class name to match (or null/undefined to match any node regardless of class name).
Returns:   The first ancestor that matches the passed criteria, or null if none match.
code »
goog.dom.getCompatMode()
Returns the compatMode of the document.
Returns:   The result is either CSS1Compat or BackCompat.
code »
goog.dom.getDocument()
Gets the document object being used by the dom library.
Returns:   Document object.
code »
goog.dom.getDocumentHeight()
Calculates the height of the document.
Returns:   The height of the current document.
code »
goog.dom.getDocumentHeight_(win)
Calculates the height of the document of the given window. Function code copied from the opensocial gadget api: gadgets.window.adjustHeight(opt_height)
Arguments:
win :
The window whose document height to retrieve.
Returns:   The height of the document of the given window.
code »
goog.dom.getDocumentScroll()
Gets the document scroll distance as a coordinate object.
Returns:   Object with values 'x' and 'y'.
code »
goog.dom.getDocumentScrollElement()
Gets the document scroll element.
Returns:   Scrolling element.
code »
goog.dom.getDocumentScrollElement_(doc)
Helper for {@code getDocumentScrollElement}.
Arguments:
doc :
The document to get the scroll element for.
Returns:   Scrolling element.
code »
goog.dom.getDocumentScroll_(doc)
Helper for {@code getDocumentScroll}.
Arguments:
doc :
The document to get the scroll for.
Returns:   Object with values 'x' and 'y'.
code »
goog.dom.getDomHelper(opt_element)
Gets the DomHelper object for the document where the element resides.
Arguments:
opt_element :
If present, gets the DomHelper for this element.
Returns:   The DomHelper.
code »
goog.dom.getElement(element)
Alias for getElementById. If a DOM node is passed in then we just return that.
Arguments:
element :
Element ID or a DOM node.
Returns:   The element with the given ID, or the node passed in.
code »
goog.dom.getElementsByTagNameAndClass(opt_tagopt_classopt_el)
{length: number}
Looks up elements by both tag and class name, using browser native functions ({@code querySelectorAll}, {@code getElementsByTagName} or {@code getElementsByClassName}) where possible. This function is a useful, if limited, way of collecting a list of DOM elements with certain characteristics. {@code goog.dom.query} offers a more powerful and general solution which allows matching on CSS3 selector expressions, but at increased cost in code size. If all you need is particular tags belonging to a single class, this function is fast and sleek.
Arguments:
opt_tag :
Element tag name.
opt_class :
Optional class name.
opt_el :
Optional element to look in.
Returns: 
{length: number}
  Array-like list of elements (only a length property and numerical indices are guaranteed to exist).
code »
goog.dom.getElementsByTagNameAndClass_(docopt_tagopt_classopt_el)
{length: number}
Helper for {@code getElementsByTagNameAndClass}.
Arguments:
doc :
The document to get the elements in.
opt_tag :
Element tag name.
opt_class :
Optional class name.
opt_el :
Optional element to look in.
Returns: 
{length: number}
  Array-like list of elements (only a length property and numerical indices are guaranteed to exist).
code »
goog.dom.getFirstElementChild(node)
Returns the first child node that is an element.
Arguments:
node :
The node to get the first child element of.
Returns:   The first child node of {@code node} that is an element.
code »
goog.dom.getFrameContentDocument(frame)
Cross-browser function for getting the document element of a frame or iframe.
Arguments:
frame :
Frame element.
Returns:   The frame content document.
code »
goog.dom.getFrameContentWindow(frame)
Cross-browser function for getting the window of a frame or iframe.
Arguments:
frame :
HTMLIFrameElement | HTMLFrameElement
Frame element.
Returns:   The window associated with the given frame.
code »
goog.dom.getLastElementChild(node)
Returns the last child node that is an element.
Arguments:
node :
The node to get the last child element of.
Returns:   The last child node of {@code node} that is an element.
code »
goog.dom.getNextElementNode_(nodeforward)
Returns the first node that is an element in the specified direction, starting with {@code node}.
Arguments:
node :
The node to get the next element from.
forward :
Whether to look forwards or backwards.
Returns:   The first element.
code »
goog.dom.getNextElementSibling(node)
Returns the first next sibling that is an element.
Arguments:
node :
The node to get the next sibling element of.
Returns:   The next sibling of {@code node} that is an element.
code »
goog.dom.getNodeAtOffset(parentoffsetopt_result)
Returns the node at a given offset in a parent node. If an object is provided for the optional third parameter, the node and the remainder of the offset will stored as properties of this object.
Arguments:
parent :
The parent node.
offset :
The offset into the parent node.
opt_result :
Object to be used to store the return value. The return value will be stored in the form {node: Node, remainder: number} if this object is provided.
Returns:   The node at the given offset.
code »
goog.dom.getNodeTextLength(node)
Returns the text length of the text contained in a node, without markup. This is equivalent to the selection length if the node was selected, or the number of cursor movements to traverse the node. Images & BRs take one space. New lines are ignored.
Arguments:
node :
The node whose text content length is being calculated.
Returns:   The length of {@code node}'s text content.
code »
goog.dom.getNodeTextOffset(nodeopt_offsetParent)
Returns the text offset of a node relative to one of its ancestors. The text length is the same as the length calculated by goog.dom.getNodeTextLength.
Arguments:
node :
The node whose offset is being calculated.
opt_offsetParent :
The node relative to which the offset will be calculated. Defaults to the node's owner document's body.
Returns:   The text offset.
code »
goog.dom.getOuterHtml(element)
Gets the outerHTML of a node, which islike innerHTML, except that it actually contains the HTML of the node itself.
Arguments:
element :
The element to get the HTML of.
Returns:   The outerHTML of the given element.
code »
goog.dom.getOwnerDocument(node)
Returns the owner document for a node.
Arguments:
node :
The node to get the document for.
Returns:   The document owning the node.
code »
goog.dom.getPageScroll(opt_window)
Gets the page scroll distance as a coordinate object.
Arguments:
opt_window :
Optional window element to test.
Returns:   Object with values 'x' and 'y'.
code »
goog.dom.getPreviousElementSibling(node)
Returns the first previous sibling that is an element.
Arguments:
node :
The node to get the previous sibling element of.
Returns:   The first previous sibling of {@code node} that is an element.
code »
goog.dom.getRawTextContent(node)
Returns the text content of the current node, without markup. Unlike {@code getTextContent} this method does not collapse whitespaces or normalize lines breaks.
Arguments:
node :
The node from which we are getting content.
Returns:   The raw text content.
code »
goog.dom.getTextContent(node)
Returns the text content of the current node, without markup and invisible symbols. New lines are stripped and whitespace is collapsed, such that each character would be visible. In browsers that support it, innerText is used. Other browsers attempt to simulate it via node traversal. Line breaks are canonicalized in IE.
Arguments:
node :
The node from which we are getting content.
Returns:   The text content.
code »
goog.dom.getTextContent_(nodebufnormalizeWhitespace)
Recursive support function for text content retrieval.
Arguments:
node :
The node from which we are getting content.
buf :
string buffer.
normalizeWhitespace :
Whether to normalize whitespace.
code »
goog.dom.getViewportSize(opt_window)
Gets the dimensions of the viewport. Gecko Standards mode: docEl.clientWidth Width of viewport excluding scrollbar. win.innerWidth Width of viewport including scrollbar. body.clientWidth Width of body element. docEl.clientHeight Height of viewport excluding scrollbar. win.innerHeight Height of viewport including scrollbar. body.clientHeight Height of document. Gecko Backwards compatible mode: docEl.clientWidth Width of viewport excluding scrollbar. win.innerWidth Width of viewport including scrollbar. body.clientWidth Width of viewport excluding scrollbar. docEl.clientHeight Height of document. win.innerHeight Height of viewport including scrollbar. body.clientHeight Height of viewport excluding scrollbar. IE6/7 Standards mode: docEl.clientWidth Width of viewport excluding scrollbar. win.innerWidth Undefined. body.clientWidth Width of body element. docEl.clientHeight Height of viewport excluding scrollbar. win.innerHeight Undefined. body.clientHeight Height of document element. IE5 + IE6/7 Backwards compatible mode: docEl.clientWidth 0. win.innerWidth Undefined. body.clientWidth Width of viewport excluding scrollbar. docEl.clientHeight 0. win.innerHeight Undefined. body.clientHeight Height of viewport excluding scrollbar. Opera 9 Standards and backwards compatible mode: docEl.clientWidth Width of viewport excluding scrollbar. win.innerWidth Width of viewport including scrollbar. body.clientWidth Width of viewport excluding scrollbar. docEl.clientHeight Height of document. win.innerHeight Height of viewport including scrollbar. body.clientHeight Height of viewport excluding scrollbar. WebKit: Safari 2 docEl.clientHeight Same as scrollHeight. docEl.clientWidth Same as innerWidth. win.innerWidth Width of viewport excluding scrollbar. win.innerHeight Height of the viewport including scrollbar. frame.innerHeight Height of the viewport exluding scrollbar. Safari 3 (tested in 522) docEl.clientWidth Width of viewport excluding scrollbar. docEl.clientHeight Height of viewport excluding scrollbar in strict mode. body.clientHeight Height of viewport excluding scrollbar in quirks mode.
Arguments:
opt_window :
Optional window element to test.
Returns:   Object with values 'width' and 'height'.
code »
goog.dom.getViewportSize_(win)
Helper for {@code getViewportSize}.
Arguments:
win :
The window to get the view port size for.
Returns:   Object with values 'width' and 'height'.
code »
goog.dom.getWindow(opt_doc)
Gets the window object associated with the given document.
Arguments:
opt_doc :
Document object to get window for.
Returns:   The window associated with the given document.
code »
goog.dom.getWindow_(doc)
Helper for {@code getWindow}.
Arguments:
doc :
Document object to get window for.
Returns:   The window associated with the given document.
code »
goog.dom.htmlToDocumentFragment(htmlString)
Converts an HTML string into a document fragment.
Arguments:
htmlString :
The HTML string to convert.
Returns:   The resulting document fragment.
code »
goog.dom.htmlToDocumentFragment_(dochtmlString)
Helper for {@code htmlToDocumentFragment}.
Arguments:
doc :
The document.
htmlString :
The HTML string to convert.
Returns:   The resulting document fragment.
code »
goog.dom.insertSiblingAfter(newNoderefNode)
Inserts a new node after an existing reference node (i.e. as the next sibling). If the reference node has no parent, then does nothing.
Arguments:
newNode :
Node to insert.
refNode :
Reference node to insert after.
code »
goog.dom.insertSiblingBefore(newNoderefNode)
Inserts a new node before an existing reference node (i.e. as the previous sibling). If the reference node has no parent, then does nothing.
Arguments:
newNode :
Node to insert.
refNode :
Reference node to insert before.
code »
goog.dom.isCss1CompatMode()
Returns true if the browser is in "CSS1-compatible" (standards-compliant) mode, false otherwise.
Returns:   True if in CSS1-compatible mode.
code »
goog.dom.isCss1CompatMode_(doc)
Returns true if the browser is in "CSS1-compatible" (standards-compliant) mode, false otherwise.
Arguments:
doc :
The document to check.
Returns:   True if in CSS1-compatible mode.
code »
goog.dom.isFocusableTabIndex(element)
Returns true if the element has a tab index that allows it to receive keyboard focus (tabIndex >= 0), false otherwise. Note that form elements natively support keyboard focus, even if they have no tab index. See http://go/tabindex for more info.
Arguments:
element :
Element to check.
Returns:   Whether the element has a tab index that allows keyboard focus.
code »
goog.dom.isNodeLike(obj)
Whether the object looks like a DOM node.
Arguments:
obj :
The object being tested for node likeness.
Returns:   Whether the object looks like a DOM node.
code »
goog.dom.isNodeList(val)
Returns true if the object is a {@code NodeList}. To qualify as a NodeList, the object must have a numeric length property and an item function (which has type 'string' on IE for some reason).
Arguments:
val :
Object to test.
Returns:   Whether the object is a NodeList.
code »
goog.dom.removeChildren(node)
Removes all the child nodes on a DOM node.
Arguments:
node :
Node to remove children from.
code »
goog.dom.removeNode(node)
Removes a node from its parent.
Arguments:
node :
The node to remove.
Returns:   The node removed if removed; else, null.
code »
goog.dom.replaceNode(newNodeoldNode)
Replaces a node in the DOM tree. Will do nothing if {@code oldNode} has no parent.
Arguments:
newNode :
Node to insert.
oldNode :
Node to replace.
code »
goog.dom.setFocusableTabIndex(elementenable)
Enables or disables keyboard focus support on the element via its tab index. Only elements for which {@link goog.dom.isFocusableTabIndex} returns true (or elements that natively support keyboard focus, like form elements) can receive keyboard focus. See http://go/tabindex for more info.
Arguments:
element :
Element whose tab index is to be changed.
enable :
Whether to set or remove a tab index on the element that supports keyboard focus.
code »
goog.dom.setProperties(elementproperties)
Sets multiple properties on a node.
Arguments:
element :
DOM node to set properties on.
properties :
Hash of property:value pairs.
code »
goog.dom.setTextContent(elementtext)
Cross-browser function for setting the text content of an element.
Arguments:
element :
The element to change the text content of.
text :
The string that should replace the current element content.
code »

Global Properties

goog.dom.BAD_CONTAINS_WEBKIT_ :
Safari contains is broken, but appears to be fixed in WebKit 522+
Code »
goog.dom.COMPAT_MODE_KNOWN_ :
Whether we know the compatibility mode at compile time.
Code »
goog.dom.DIRECT_ATTRIBUTE_MAP_ :
Map of attributes that should be set using element.setAttribute(key, val) instead of element[key] = val. Used by goog.dom.setProperties.
Code »
goog.dom.PREDEFINED_TAG_VALUES_ :
Map of tags which have predefined values with regard to whitespace.
Code »
goog.dom.TAGS_TO_IGNORE_ :
Map of tags whose content to ignore when calculating text length.
Code »

Directory dom

File Reference