# Yahoo! UI Library

### dom

Yahoo! UI Library > dom > YAHOO.util.Dom

## Class YAHOO.util.Dom

Provides helper methods for DOM elements.

### Methods

#### addClass

`void addClass ( el , className )`
Adds a class name to a given element or collection of elements.

Parameters:
  `el <String | HTMLElement | Array>` The element or collection to add the class to
  `className <String>` the class name to add to the class attribute

Returns: `void`

#### batch

`HTMLElement | Array batch ( el , method , o , override )`
Returns an array of elements that have had the supplied method applied. The method is called with the element(s) as the first arg, and the optional param as the second ( method(el, o) ).

Parameters:
  `el <String | HTMLElement | Array>` (optional) An element or array of elements to apply the method to
  `method <Function>` The method to apply to the element(s)
  `o <Any>` (optional) An optional arg that is passed to the supplied method
  `override <Boolean>` (optional) Whether or not to override the scope of "method" with "o"

Returns: `HTMLElement | Array`
  The element(s) with the method applied

#### generateId

`String | Array generateId ( el , prefix )`
Generates a unique ID

Parameters:
  `el <String | HTMLElement | Array>` (optional) An optional element array of elements to add an ID to (no ID is added if one is already present).
  `prefix <String>` (optional) an optional prefix to use (defaults to "yui-gen").

Returns: `String | Array`
  The generated ID, or array of generated IDs (or original ID if already present on an element)

#### get

`HTMLElement | Array get ( el )`
Returns an HTMLElement reference.

Parameters:
  `el <String | HTMLElement |Array>` Accepts a string to use as an ID for getting a DOM reference, an actual DOM reference, or an Array of IDs and/or HTMLElements.

Returns: `HTMLElement | Array`
  A DOM reference to an HTML element or an array of HTMLElements.

#### getClientHeight

`Int getClientHeight ( )`
Returns the height of the client (viewport).

Returns: `Int`
  The height of the viewable area of the page.

Deprecated Now using getViewportHeight. This interface left intact for back compat.

#### getClientWidth

`Int getClientWidth ( )`
Returns the width of the client (viewport).

Returns: `Int`
  The width of the viewable area of the page.

Deprecated Now using getViewportWidth. This interface left intact for back compat.

#### getDocumentHeight

`Int getDocumentHeight ( )`
Returns the height of the document.

Returns: `Int`
  The height of the actual document (which includes the body and its margin).

#### getDocumentWidth

`Int getDocumentWidth ( )`
Returns the width of the document.

Returns: `Int`
  The width of the actual document (which includes the body and its margin).

#### getElementsBy

`void getElementsBy ( method , tag , root )`
Returns a array of HTMLElements that pass the test applied by supplied boolean method. For optimized performance, include a tag and/or root node when possible.

Parameters:
  `method <Function>` - A boolean method for testing elements which receives the element as its only argument.
  `tag <String>` (optional) The tag name of the elements being collected
  `root <String | HTMLElement>` (optional) The HTMLElement or an ID to use as the starting point

Returns: `void`

#### getElementsByClassName

`Array getElementsByClassName ( className , tag , root )`
Returns a array of HTMLElements with the given class. For optimized performance, include a tag and/or root node when possible.

Parameters:
  `className <String>` The class name to match against
  `tag <String>` (optional) The tag name of the elements being collected
  `root <String | HTMLElement>` (optional) The HTMLElement or an ID to use as the starting point

Returns: `Array`
  An array of elements that have the given class name

#### getRegion

`Region | Array getRegion ( el )`
Returns the region position of the given element. The element must be part of the DOM tree to have a region (display:none or elements not appended return false).

Parameters:
  `el <String | HTMLElement | Array>` Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.

Returns: `Region | Array`
  A Region or array of Region instances containing "top, left, bottom, right" member data.

#### getStyle

`String | Array getStyle ( el , property )`
Normalizes currentStyle and ComputedStyle.

Parameters:
  `el <String | HTMLElement |Array>` Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
  `property <String>` The style property whose value is returned.

Returns: `String | Array`
  The current value of the style property for the element(s).

#### getViewportHeight

`Int getViewportHeight ( )`
Returns the current height of the viewport.

Returns: `Int`
  The height of the viewable area of the page (excludes scrollbars).

#### getViewportWidth

`Int getViewportWidth ( )`
Returns the current width of the viewport.

Returns: `Int`
  The width of the viewable area of the page (excludes scrollbars).

#### getX

`String | Array getX ( el )`
Gets the current X position of an element based on page coordinates. The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).

Parameters:
  `el <String | HTMLElement | Array>` Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements

Returns: `String | Array`
  The X position of the element(s)

#### getXY

`Array getXY ( el )`
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:
  `el <String | HTMLElement | Array>` Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements

Returns: `Array`
  The XY position of the element(s)

#### getY

`String | Array getY ( el )`
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:
  `el <String | HTMLElement | Array>` Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements

Returns: `String | Array`
  The Y position of the element(s)

#### hasClass

`Boolean | Array hasClass ( el , className )`
Determines whether an HTMLElement has the given className.

Parameters:
  `el <String | HTMLElement | Array>` The element or collection to test
  `className <String>` the class name to search for

Returns: `Boolean | Array`
  A boolean value or array of boolean values

#### inDocument

`Boolean inDocument ( el )`
Determines whether an HTMLElement is present in the current document.

Parameters:
  `el <String | HTMLElement>` The element to search for

Returns: `Boolean`
  Whether or not the element is present in the current document

#### isAncestor

`Boolean isAncestor ( haystack , needle )`
Determines whether an HTMLElement is an ancestor of another HTML element in the DOM hierarchy.

Parameters:
  `haystack <String | HTMLElement>` The possible ancestor
  `needle <String | HTMLElement>` The possible descendent

Returns: `Boolean`
  Whether or not the haystack is an ancestor of needle

#### removeClass

`void removeClass ( el , className )`
Removes a class name from a given element or collection of elements.

Parameters:
  `el <String | HTMLElement | Array>` The element or collection to remove the class from
  `className <String>` the class name to remove from the class attribute

Returns: `void`

#### replaceClass

`void replaceClass ( el , oldClassName , newClassName )`
Replace a class with another class for a given element or collection of elements. If no oldClassName is present, the newClassName is simply added.

Parameters:
  `el <String | HTMLElement | Array>` The element or collection to remove the class from
  `oldClassName <String>` the class name to be replaced
  `newClassName <String>` the class name that will be replacing the old class name

Returns: `void`

#### setStyle

`void setStyle ( el , property , val )`
Wrapper for setting style properties of HTMLElements. Normalizes "opacity" across modern browsers.

Parameters:
  `el <String | HTMLElement | Array>` Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
  `property <String>` The style property to be set.
  `val <String>` The value to apply to the given property.

Returns: `void`

#### setX

`void setX ( el , x )`
Set the X position of an html element in page coordinates, regardless of how the element is positioned. The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).

Parameters:
  `el <String | HTMLElement | Array>` Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
  `x <Int>` The value to use as the X coordinate for the element(s).

Returns: `void`

#### setXY

`void setXY ( el , pos , noRetry )`
Set the 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).

Parameters:
  `el <String | HTMLElement | Array>` Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements
  `pos <Array>` Contains X & Y values for new position (coordinates are page-based)
  `noRetry <Boolean>` By default we try and set the position a second time if the first fails

Returns: `void`

#### setY

`void setY ( el , x )`
Set the Y position of an html element in page coordinates, regardless of how the element is positioned. The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).

Parameters:
  `el <String | HTMLElement | Array>` Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
  `x <Int>` To use as the Y coordinate for the element(s).

Returns: `void`

Copyright © 2006 Yahoo! Inc. All rights reserved.