The Element class provides a wrapper for HTMLElements in the DOM and makes simpler common tasks such as adding listeners, manipulating the DOM, and setting and getting attributes. The Element Utility provides a wrapper so that these actions can be performed directly on an Element instance.
Note: Although the Element Utility is a dependency for the TabView Control which has reached general availability (GA), we continue to designate Element as a beta component for the time being. That designation signals to you as an implementer that, although we're committed to and using this component, we have not locked down its API. Be aware that implementing Element directly at this point could require changes to your code as we evolve Element's interface in subsequent releases.
To use the Element Utility, include the following source files in your web page with the <script> tag:
Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader write out the full HTML for including the precise files you need for your implementation.
Note: If you wish to include this component via the YUI Loader, its module name is element. (Click here for the full list of module names for YUI Loader.)
Where these files come from: The files included using the text above will be served from Yahoo! servers; see "Serving YUI Files from Yahoo!" for important information about this service. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug versions of YUI JavaScript files, please download the library distribution and host the files on your own server.
Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.
To create a new Element instance, pass either the ID of the element or a reference to the HTMLElement to the Element constructor:
In this example, a new instance of YAHOO.util.Element is created for an element whose id is foo. If foo is not yet available in the document, event listeners may still be added, and attibutes may be set; the Element Utility will automatically defer those actions until the element becomes available. Be aware, however, that true manipulation of the element's DOM may not be performed until the contentReady event has fired. (Note: onContentReady is a YUI construct supported by the YUI Event Utility.)
Let's look at these steps in a code example:
This section describes several common uses of Element. It contains these subsections:
All Element events are applied using the addListener method (alised as on and can be removed with removeListener.
The above example invokes the handleClick function when the element whose id attribute is foo is clicked. The handler then removes the listener, so subsequent clicks will not fire the event.
All of the attributes and properties of the HTMLElement can be accessed via the set and get methods of Element. Additionally, the actual HTMLElement bound to the Element instance can be retrieved via get('element').
The above example alerts the element's id attribute and the number of childNodes owned by that element.
If the element is not yet available, you will need to defer accessing the HTMLElement until the contentReady event fires. You can write deferral code as follows:
Most common Dom methods can be applied directy to the Element instance itself:
Element instances can be styled directly via the setStyle method; style properties can be retrieved via getStyle:
The YUI Library and related topics are discussed on the on the ydn-javascript mailing list.
In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.
The YUI Library's public bug tracking and feature request repositories are located on the YUI SourceForge project site. Before filing new feature requests or bug reports, please review our reporting guidelines.














Copyright © 2008 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings