<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web 3.0, 6 Bladed Razors, 7 Minute Abs &#187; DOM</title>
	<atom:link href="http://www.zachleat.com/web/tag/dom/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zachleat.com/web</link>
	<description></description>
	<lastBuildDate>Mon, 06 Feb 2012 17:09:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Write More Efficient Javascript in YUI with CSS Selectors</title>
		<link>http://www.zachleat.com/web/write-more-efficient-javascript-in-yui-with-css-selectors/</link>
		<comments>http://www.zachleat.com/web/write-more-efficient-javascript-in-yui-with-css-selectors/#comments</comments>
		<pubDate>Mon, 03 Sep 2007 22:36:05 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[CSS Selectors]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[ExtJS]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/2007/09/03/write-more-efficient-javascript-in-yui-with-css-selectors/</guid>
		<description><![CDATA[This is an updated version of a previous article entitled Using DOM Query Libraries in YUI for the new version of YAHOO.util.Dom included with YUI 2.3.0 as well as including support for passing context nodes into Dom functions. Here we are again. I just love those CSS Selectors. If you haven&#8217;t read Part One of [...]]]></description>
			<content:encoded><![CDATA[<p><em>This is an updated version of a previous article entitled <a href="http://www.zachleat.com/web/2007/07/30/using-dom-query-libraries-in-yui/">Using DOM Query Libraries in YUI</a> for the new version of YAHOO.util.Dom included with YUI 2.3.0 as well as including support for passing context nodes into Dom functions.</em></p>
<p>Here we are again.  I just love those CSS Selectors.  If you haven&#8217;t read Part One of the series linked above, I would encourage it to get a little background on why we&#8217;re attempting this approach.</p>
<h1>Download the appropriate files:</h1>
<h1>Get the easy <strong>ALL-IN-ONE file</strong>: <a href="/Lib/Y2/Y2.js">Y2.js</a> (31 KB original source) OR <a href="/Lib/Y2/Y2-p.js">Y2-p.js</a> (11 KB minimized)</h1>
<h2>Get the files separately (if you want to use a custom selector library, or have already included DomQuery or jQuery): <a href="/Lib/Y2/Y2-solo.js">Y2-solo.js</a> (9 KB original source) OR <a href="/Lib/Y2/Y2-solo-p.js">Y2-solo-p.js</a> (4 KB minimized)</h2>
<p>Also, first you&#8217;ll need to get Jack&#8217;s nice DomQuery class.</p>
<p>Download: <a href="/Lib/Y2/DomQuery.js">Jack Slocum&#8217;s DomQuery Standalone File (24 KB)</a> 1.0 Alpha 3 &#8211; Rev 4<br />
Download: <a href="/Lib/Y2/DomQuery-packer.js">Jack Slocum&#8217;s DomQuery Standalone File Packed (7 KB)</a> 1.0 Alpha 3 &#8211; Rev 4</p>
<p>You can use the jDomQuery standalone, or any jQuery version, (instead of DomQuery) linked in the previous article, but there is an unresolved bug with the getRegion method since jQuery doesn&#8217;t return actual Array&#8217;s.  But if you&#8217;re not using that method, have at it.</p>
<h1>What can I do with it?</h1>
<p>I have not included ALL functions in this class, only the ones that take nodes in as arguments.  If you use these functions, you are encouraged to use the unmodified versions contained in YAHOO.util.Dom.  Functions not included here: getClientHeight, getClientWidth, getDocumentHeight, getDocumentScrollLeft, getDocumentScrollTop, getDocumentWidth, getViewportHeight, getViewportWidth.</p>
<p>Let&#8217;s see some documentation on this mother (if you&#8217;re wondering what the original functions do, see the <a href="http://developer.yahoo.com/yui/docs/YAHOO.util.Dom.html">original documentation</a>).</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// this is the test context node we're going to pass in</span>
<span style="color: #003366; font-weight: bold;">var</span> context <span style="color: #339933;">=</span> YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'context'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>addClass</h1>
<p>Adds a CSS class to first matching node.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns void</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'div div:last'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'myClass'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div div:last'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #3366CC;">'myClassContext'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>addClassAll</h1>
<p>Adds a CSS class to all matching nodes.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns void</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">addClassAll</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'div div:last'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'myClass'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">addClassAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div div:last'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #3366CC;">'myClassContext'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>hasClass</h1>
<p>Tests the first result node returned to see if it has a CSS class.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Booleans.</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div div:last'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'myClass'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div div:last'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #3366CC;">'myClassContext'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>hasClassAll</h1>
<p>Tests all matching nodes to see if they have a CSS class.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Array of Booleans.</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">hasClassAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div div:last'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'myClass'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">hasClassAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div div:last'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #3366CC;">'myClassContext'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>removeClass</h1>
<p>Removes a CSS class from first matched node.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Boolean (misdocumented on the YAHOO.util.Dom page)</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'third'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span><span style="color: #3366CC;">'third'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>removeClassAll</h1>
<p>Removes a CSS class from all matched nodes.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Array of Booleans (misdocumented on the YAHOO.util.Dom page)</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">removeClassAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'third'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">removeClassAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span><span style="color: #3366CC;">'third'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>replaceClass</h1>
<p>Replaces a CSS class on first matched node with a new CSS class, or adds it if the old CSS class doesn&#8217;t exist.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Boolean (misdocumented on the YAHOO.util.Dom page)</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">replaceClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.fourth'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'fourth'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'third'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">replaceClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.fourth'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span><span style="color: #3366CC;">'fourth'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'third'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>replaceClassAll</h1>
<p>Replaces a CSS class on all matched nodes with a new CSS class, or adds it if the old CSS class doesn&#8217;t exist.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Array of Booleans (misdocumented on the YAHOO.util.Dom page)</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">replaceClassAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.fourth'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'fourth'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'third'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">replaceClassAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.fourth'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span><span style="color: #3366CC;">'fourth'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'third'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>get</h1>
<p>Get first matched node.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div b'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div b'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getAll</h1>
<p>Get all matched nodes.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Array of HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div b'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div b'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>batch</h1>
<p>Executes a function against first matched node.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns single return value from the function</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">batch</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second b'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> el<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">batch</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div b'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> el<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>batchAll</h1>
<p>Executes a function against all matched nodes.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Array of return values from the function</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">batchAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second b'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> el<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">batchAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div b'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> el<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>generateId</h1>
<p>Generates and assigns a unique ID attribute if not present on first matched element.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns String (new ID attribute)</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">generateId</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.first'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">generateId</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>generateIdAll</h1>
<p>Generates and assigns a unique ID attribute if not present on matched elements.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Array of String (new ID attributes)</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">generateIdAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.first'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">generateIdAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getChildren</h1>
<p>Gets all HTMLElement children of the first matched node (does not include text nodes or whitespace)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Array of HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getChildrenBy</h1>
<p>Returns all HTMLElement children of the first matched node that pass a Boolean function test. (does not include text nodes or whitespace)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Array of HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getChildrenBy</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> el.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'theMiddleB'</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getChildrenBy</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> el.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'theMiddleB'</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getAncestorBy</h1>
<p>Returns the first HTMLElement ancestor of the first matched node that passes a Boolean function test. (does not include text nodes or whitespace)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getAncestorBy</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> el.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'context'</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getAncestorBy</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> el.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'context'</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getAncestorByClassName</h1>
<p>Returns the first HTMLElement ancestor of the first matched node that has a CSS Class. (does not include text nodes or whitespace)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getAncestorByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'contextClass'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getAncestorByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #3366CC;">'contextClass'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getAncestorByClassName</h1>
<p>Returns the first HTMLElement ancestor of the first matched node that is a certain tag (does not include text nodes or whitespace)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getAncestorByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getAncestorByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getFirstChild</h1>
<p>Returns the first HTMLElement child of the first matched node (does not include text nodes or whitespace)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getFirstChild</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getFirstChild</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getFirstChildBy</h1>
<p>Returns the first HTMLElement child that passes a boolean function test from the first matched node (does not include text nodes or whitespace)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getFirstChildBy</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> el.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'theMiddleB'</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getFirstChildBy</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> el.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'theMiddleB'</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getLastChild</h1>
<p>Returns the last HTMLElement child of the first matched node (does not include text nodes or whitespace)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getLastChild</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getLastChild</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getLastChildBy</h1>
<p>Returns the last HTMLElement child that passes a boolean function test from the first matched node (does not include text nodes or whitespace)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getLastChildBy</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> el.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'theMiddleB'</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getLastChildBy</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> el.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'theMiddleB'</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getNextSibling</h1>
<p>Returns the next HTMLElement sibling of the first matched node (does not include text nodes or whitespace)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getNextSibling</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getNextSibling</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getNextSiblingBy</h1>
<p>Returns the next HTMLElement sibling that passes a boolean function test from the first matched node (does not include text nodes or whitespace)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getNextSiblingBy</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> el.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'third'</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getNextSiblingBy</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span>el<span style="color: #339933;">,</span><span style="color: #3366CC;">'third'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getPreviousSibling</h1>
<p>Returns the previous HTMLElement sibling of the first matched node (does not include text nodes or whitespace)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getPreviousSibling</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getPreviousSibling</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getPreviousSiblingBy</h1>
<p>Returns the previous HTMLElement sibling that passes a boolean function test from the first matched node (does not include text nodes or whitespace)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getPreviousSiblingBy</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> el.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'third'</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getPreviousSiblingBy</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span>el<span style="color: #339933;">,</span><span style="color: #3366CC;">'third'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getRegion</h1>
<p>Gets the <a href="http://developer.yahoo.com/yui/docs/YAHOO.util.Region.html">Region</a> containing the first matched element.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns &lt;a href=&quot;http://developer.yahoo.com/yui/docs/YAHOO.util.Region.html&quot;&gt;YAHOO.util.Region&lt;/a&gt;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getRegion</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getRegion</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getRegionAll</h1>
<p>Gets all <a href="http://developer.yahoo.com/yui/docs/YAHOO.util.Region.html">Regions</a> containing all matched elements.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Array of &lt;a href=&quot;http://developer.yahoo.com/yui/docs/YAHOO.util.Region.html&quot;&gt;YAHOO.util.Region&lt;/a&gt;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getRegionAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.myClass'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getRegionAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.myClass'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>setStyle</h1>
<p>Sets the style for the first matched node</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns void</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'border'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'1px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #3366CC;">'border'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'10px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>setStyleAll</h1>
<p>Sets style for all matched nodes</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns void</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setStyleAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'border'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'1px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setStyleAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #3366CC;">'border'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'10px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getStyle</h1>
<p>Gets a specific style property from the first matched node</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns String of specific style property</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getStyle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'border'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getStyle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #3366CC;">'border'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getStyleAll</h1>
<p>Gets a specific style property from all matched nodes</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Array of Strings of specific style property</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getStyleAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'border'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getStyleAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #3366CC;">'border'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>setX, setY, setXY</h1>
<p>Sets the horizontal placement (X), vertical placement (Y), or both (XY) of the first matched element</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns void</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setX</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setX</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setY</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setY</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setXY</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">120</span><span style="color: #339933;">,</span><span style="color: #CC0000;">120</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setXY</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">120</span><span style="color: #339933;">,</span><span style="color: #CC0000;">120</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>setXAll, setYAll, setXYAll</h1>
<p>Sets the horizontal placement (X), vertical placement (Y), or both (XY) of all matched elements</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns void</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setXAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setXAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setYAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setYAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setXYAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">120</span><span style="color: #339933;">,</span><span style="color: #CC0000;">120</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setXYAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">120</span><span style="color: #339933;">,</span><span style="color: #CC0000;">120</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getX, getY, getXY</h1>
<p>Gets the horizontal placement (X), vertical placement (Y), or both (XY) of the first matched element</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Integer</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getX</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getX</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getY</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getY</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getXY</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getXY</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>getXAll, getYAll, getXYAll</h1>
<p>Gets the horizontal placement (X), vertical placement (Y), or both (XY) of all matched elements</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Array of Integers</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getXAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getXAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getYAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getYAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getXYAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getXYAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>inDocument</h1>
<p>Find out whether the first matched element is in the current document</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns Boolean</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">inDocument</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">inDocument</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>insertAfter</h1>
<p>Take the first matched node of the first selector and insert after the first matched node of the second selector</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Note that this function handles context nodes on the first or second selectors, both, or none.</span>
<span style="color: #006600; font-style: italic;">// returns the moved HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">insertAfter</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'div.fourth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">insertAfter</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span><span style="color: #3366CC;">'div.fourth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">insertAfter</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'div.fourth'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">insertAfter</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span><span style="color: #3366CC;">'div.fourth'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>insertBefore</h1>
<p>Take the first matched node of the first selector and insert before the first matched node of the second selector</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Note that this function handles context nodes on the first or second selectors, both, or none.</span>
<span style="color: #006600; font-style: italic;">// returns the moved HTMLElement</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">insertBefore</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'div.fourth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">insertBefore</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span><span style="color: #3366CC;">'div.fourth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">insertBefore</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'div.fourth'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">insertBefore</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.third'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span><span style="color: #3366CC;">'div.fourth'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>isAncestor</h1>
<p>Find out whether the first matched node of the first selector is an ancestor of the first matched node of the second selector.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Note that this function handles context nodes on the first or second selectors, both, or none.</span>
<span style="color: #006600; font-style: italic;">// return Boolean</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">isAncestor</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'b#theFirstB'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">isAncestor</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span><span style="color: #3366CC;">'b#theFirstB'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">isAncestor</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'b#theFirstB'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Y2.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">isAncestor</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.second'</span><span style="color: #339933;">,</span> context<span style="color: #339933;">,</span><span style="color: #3366CC;">'b#theFirstB'</span><span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>(Documentation is a bitch!)</p>
<h1>Additional Information</h1>
<ul>
<li><a href="http://www.zachleat.com/Projects/Y2/test.html">Run the test document.</a> (Might convert this to YUI Test framework if there is enough interest in this extension)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/write-more-efficient-javascript-in-yui-with-css-selectors/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DOMDom, easy DOM Element Creation</title>
		<link>http://www.zachleat.com/web/domdom-easy-dom-element-creation/</link>
		<comments>http://www.zachleat.com/web/domdom-easy-dom-element-creation/#comments</comments>
		<pubDate>Sun, 08 Jul 2007 04:59:31 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[CSS Selectors]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[ExtJS]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/2007/07/07/domdom-easy-dom-element-creation/</guid>
		<description><![CDATA[Generally when approaching a complex problem involving web programming in JavaScript, the first question I ask myself is &#8216;What would Jesus do?&#8217; As much as I am entertained by a mental picture of Mr. Jesus himself sitting on his Jesux Distro appending children (and parents) into his DOM, I am instead distracted by an intense [...]]]></description>
			<content:encoded><![CDATA[<p>Generally when approaching a complex problem involving web programming in JavaScript, the first question I ask myself is &#8216;What would Jesus do?&#8217;  As much as I am entertained by a mental picture of Mr. Jesus himself sitting on his <a href="http://www.geocities.com/ResearchTriangle/Node/4081/">Jesux Distro</a> appending children (and parents) into his DOM, I am instead distracted by an intense &#8220;passionate&#8221; hatred for Mel Gibson swelling in my chest.  It&#8217;s actually quite distracting to the problem I am having, but I calm myself by punching my 4 foot tall inflatable Mad Max and wonder how much time I waste doing this.  I figure it happens about twice an hour.  (Digression)</p>
<p>The DOM.  Arch-nemesis of web developers attempting to support the ultimately tiny (I think it&#8217;s down to about 5 or 8% now?) sliver of browser market share that Internet Explorer currently entails.  Let&#8217;s make it easier on ourselves and make a little package to do it for us.  Run a function, pass in an argument with a description of the DOM node(s) we wish to create, and have the package auto-correct any bugs we would have encountered during manual creation, and maybe even have it inserted or appended for us.</p>
<p>But wait, why are we doing this when there are literally 80 billion other DOM element creation classes already out there?  It&#8217;s all about syntax.  The existing packages are incredibly verbose, focusing too much on a complex object structure to describe the nodes, in some cases even having separate objects for attributes inside a single node.  Why not use the syntax we&#8217;ve already come to love in the various DOM query libraries that are available?  Why not use DOMDom? Let&#8217;s see a few examples:</p>
<p>Single Node String</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">'div'</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div&gt;&lt;/div&gt;</pre></div></div>

<p>Single Node String with ID and Classes</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">'div#id1.class1.class2'</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;id1&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;class1 class2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Single Node String for a Form Element</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">'input[name=&quot;myTextBox&quot;,type=&quot;text&quot;,maxlength=&quot;5&quot;]'</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;myTextBox&quot;</span> <span style="color: #000066;">maxlength</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>Single Node String with Style Syntax</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">'div{height=80px,color=#f90,border=1px solid #000}'</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;border: 1px solid #000; height: 80px; color: #f90&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Complex Single Node String with ID, multiple classes, Style, and Namespaced Attribute)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">'div#id1.class1.class2[style=&quot;width:60px;color:#f90&quot;,@class=&quot;class4&quot;,@att=&quot;true&quot;,@namespace:att=&quot;false&quot;]'</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;id1&quot;</span> <span style="color: #000066;">att</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">namespace:att</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;class1 class2 class4&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;width: 60px; color: #f90;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Multiple Node String: Linear (Parents with one Child)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">'div span div'</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Multiple Node String: Non-Linear (Parent with more than one Child)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'div'</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span> <span style="color: #3366CC;">'span'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'span'</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h1>How Does It Work?</h1>
<p>By default, it&#8217;s set up to do HTML Fragments (innerHTML) because they are much speedier than the manual DOM element creation (createElement).  But if you desire, you can toggle a boolean in the code and it will switch back to DOM element creation.  When in DOM element creation mode, it will account for the following browser bugs:</p>
<ul>
<li>(IE6) Standardized for attribute representation (pointer to htmlFor)</li>
<li>(IE6) Standardized case for accesskey, usemap, maxlength, and frameborder attributes.</li>
<li>(IE6) Standardized checked attribute for radio and checkboxes.</li>
<li>(IE6) Special consideration for dynamic handling of name and type attributes (on form elements).</li>
<li>(Firefox) <a href="http://developer.mozilla.org/en/docs/Whitespace_in_the_DOM">Works with whitespace that is treated as a node.</a></li>
</ul>
<h1>Syntax</h1>
<p>You should already be able to tell how to create a node from the examples above.  Here are some more things you might not have guessed:</p>
<p>Creating a node with an id</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">'div#myId'</span></pre></div></div>

<p>Creating a node with CSS classes</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">'div.class1.class2'</span></pre></div></div>

<p>Creating two nodes at the same level</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #3366CC;">'div'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'div'</span> <span style="color: #009900;">&#93;</span></pre></div></div>

<p>Creating a node with two children</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'div'</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span> <span style="color: #3366CC;">'div'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'div'</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can mix the {} and [] syntax wherever you like, but if you want a node to have non-linear children, you have to use the {} object notation.</p>
<p>Creating a text node (start the node declaration with a #, you can change this to another non-conflicting character in the code if you like)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">'#Any Text Here'</span></pre></div></div>

<p>Creating a node with attributes (the @ is optional)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">'div[class=&quot;class3&quot;,style=&quot;width:60px;color:#f90&quot;,@att=&quot;true&quot;,@namespace:att=&quot;false&quot;]'</span></pre></div></div>

<p>Creating a node with a Style Shortcut (mixing with a style attribute is handled properly)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">'div{color=#f90,border=1px solid #000}[style=&quot;height:80px;background:#fff&quot;]'</span></pre></div></div>

<p>A few notes on attributes.  Quotes are required on attributes (single or double but be consistent), but are not required in the style shortcut declaration.  Quotes are not allowed to be nested inside of attributes (a single quote cannot be inside of a double quote and vice versa).</p>
<p>And of course, all of the above can be mixed together</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">'div#myId.class1.class2{color=#f90}[customAttr=&quot;true&quot;,@customAttr2=&quot;false&quot;] div#child1 div#child2'</span></pre></div></div>

<h1>Usage</h1>
<p><strong>Appending</strong> at the end of a parent&#8217;s children:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">DOMDom.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'div'</span><span style="color: #339933;">,</span> yourParentNode <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Replacing</strong> the children of a parent:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">DOMDom.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'div'</span><span style="color: #339933;">,</span> yourParentNode <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Unshifting </strong>(inserting at the beginning of a parent&#8217;s children):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">DOMDom.<span style="color: #660066;">unshift</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'div'</span><span style="color: #339933;">,</span> yourParentNode <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Inserting</strong> before a certain integer index of a parent&#8217;s children:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">DOMDom.<span style="color: #660066;">insert</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'div'</span><span style="color: #339933;">,</span> yourParentNode<span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// must have at least 3 children, the index is 0 based, if index is null with unshift by default</span></pre></div></div>

<h1>Templates</h1>
<p>Use &lt;$var&gt; to indicate a variable, in this example &lt;$test&gt;</p>
<p><code><br />
// "Compile" the template<br />
var str = DOMDom.compile( { 'div.test span': '#Test &lt;$test&gt;' } );</p>
<p>// Use your template in some context, notice the test variable being set.<br />
for( var j = 0; j < 1000; j++ )<br />
{<br />
	// knows we're using a compiled template since we're passing in variables as a third argument.<br />
	DOMDom.append( str, d, { test: j } );<br />
}<br />
</code></p>
<h3>Benchmarks</p>
<h4>
(If you have Firebug open, make sure it's not on the HTML tab, this will slow down the benchmark significantly)</p>
<p>Most of my work here has been inspired by the DomQuery and DomHelper classes written by JavaScript rock star Jack Slocum (the guy's initials are J.S. for God's sake), so I modeled my benchmark after <a href="http://www.jackslocum.com/blog/examples/domhelper.php">his benchmark hosted on his website to test the DomHelper class</a>.  I'm running the same nodes he's testing on his website, so the results should be comparable.  You can <a href="http://www.zachleat.com/Projects/DOMDom/benchmark.html">test my benchmark for DOMDom here</a>.  Here are some results, reporting the average of 3 results with the format of an uncompiled element first and the compiled template in square brackets.<br />
</h4>
<h2>DOMDom Results</h2>
<p>Internet Explorer 6: 666 ms [328 ms]<br />
Firefox 2.0.0.4: 1880 ms [666 ms]<br />
Safari 3.0.2 [Windows]: 546 ms [151 ms]<br />
Opera 9.21: 343 ms [140 ms]</p>
<h2>Comparative numbers from Jack Slocum's DomHelper</h2>
<p>Internet Explorer 6: 2458 ms [677 ms]<br />
Firefox 2.0.0.4: 672 ms [458 ms]<br />
Safari 3.0.2 [Windows]: 291 ms [119 ms]<br />
Opera 9.21: 370 ms [166 ms]</p>
<p>The thing to take away from this is the question of why Satan is haunting my benchmarks?  Two 666 averages?  Anyway, DOMDom is quite a bit faster in the most popular browser, Internet Explorer, although I haven't tested it on IE7 yet.  In Firefox, the opposite is true, with DomHelper taking the lead.  Opera is comparable and Safari is faster in DomHelper as well.  You can run your own tests using the links above.</p>
<h1>Dependencies</h1>
<p>This library was built to work with Yahoo User Interface (YUI), but could be trivially ported to another library by changing the function dependencies listed in the ADAPTER variable in the code.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> ADAPTER <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	setStyle<span style="color: #339933;">:</span> YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">setStyle</span><span style="color: #339933;">,</span>
	addClass<span style="color: #339933;">:</span> YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">addClass</span><span style="color: #339933;">,</span>
	isString<span style="color: #339933;">:</span> YAHOO.<span style="color: #660066;">lang</span>.<span style="color: #660066;">isString</span><span style="color: #339933;">,</span>
	isArray<span style="color: #339933;">:</span> YAHOO.<span style="color: #660066;">lang</span>.<span style="color: #660066;">isArray</span><span style="color: #339933;">,</span>
	isNumber<span style="color: #339933;">:</span> YAHOO.<span style="color: #660066;">lang</span>.<span style="color: #660066;">isNumber</span><span style="color: #339933;">,</span>
	isObject<span style="color: #339933;">:</span> YAHOO.<span style="color: #660066;">lang</span>.<span style="color: #660066;">isObject</span><span style="color: #339933;">,</span>
	get<span style="color: #339933;">:</span> YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">get</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// to port, change these references</span></pre></div></div>

<p>If you're still reading this encyclopedia, here are some links:</p>
<ul>
<li><span style="font-size: 200%"><a href="http://www.zachleat.com/Projects/DOMDom/DOMDom.js">DOWNLOAD DOMDom</a></span></li>
<li><a href="http://www.zachleat.com/Projects/DOMDom/DOMDom-min-jsmin.js">DOWNLOAD DOMDom Minimized with JSMIN (10KB)</a></li>
<li><a href="http://www.zachleat.com/Projects/DOMDom/DOMDom-min-packer.js">DOWNLOAD DOMDom Minimized with Packer (6KB)</a></li>
<li><a href="http://www.zachleat.com/Projects/DOMDom/benchmark.html">See the benchmark</a></li>
<li><a href="http://www.zachleat.com/Projects/DOMDom/tests.html">See the test file with a bunch of examples of syntax</a></li>
</ul>
<p><a href="http://www.encyclopedia.com/doc/1O27-dom1.html"><em>-dom suffix denoting condition or state, as in freedom, wisdom, or DOMDom</em></a></p>
<p><strong>Update</strong>: changed the variable syntax to allow variables inside of nodes (not just text).</h3>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/domdom-easy-dom-element-creation/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

