<?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>Chanel's Blog &#187; getelementbyid</title>
	<atom:link href="http://cmunezero.com/tag/getelementbyid/feed/" rel="self" type="application/rss+xml" />
	<link>http://cmunezero.com</link>
	<description>Where code meets love... and a lot of awkward silences...</description>
	<lastBuildDate>Tue, 11 May 2010 21:17:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Javascript Tip: getElementsByID</title>
		<link>http://cmunezero.com/2008/03/17/javascript-tip-getelementsbyid/</link>
		<comments>http://cmunezero.com/2008/03/17/javascript-tip-getelementsbyid/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 05:26:40 +0000</pubDate>
		<dc:creator>chanel</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[getelementbyid]]></category>
		<category><![CDATA[getelementsbyid]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[html document]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[javascript tip]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://cmunezero.com/2008/03/17/javascript-tip-getelementsbyid/</guid>
		<description><![CDATA[Browsers do a lousy job of providing an interface to the HTML document.  The DOM is supposed to be that interface but it is horribly slow, and clunky.  Traversing the DOM tree extensively is one of the sure-fire way to slow down your site.  In an effort to help out Javascript coders, [...]]]></description>
			<content:encoded><![CDATA[<p>Browsers do a lousy job of providing an interface to the HTML document.  The DOM is supposed to be that interface but it is horribly slow, and clunky.  Traversing the DOM tree extensively is one of the sure-fire way to slow down your site.  In an effort to help out Javascript coders, the DOM does have functions like getElementsByTagName, getElementsByClassName and getElementsByName, but they do not all work across all browsers.  This why you should create a function called <a href="http://alex.dojotoolkit.org/?p=550">getElementsByID</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> groupCache <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> getElementsById<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>groupCache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    groupCache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #003366; font-weight: bold;">var</span> nodes <span style="color: #339933;">=</span> groupCache<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> x<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> x<span style="color: #339933;">&lt;</span>nodes .<span style="color: #660066;">length</span><span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>nodes<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      nodes.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      x<span style="color: #339933;">--;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #003366; font-weight: bold;">var</span> tmpNode <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>tmpNode<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    nodes.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>tmpNode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    tmpNode.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
    tmpNode <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> nodes<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now whenever you want a collection of DOM objects, just give all of them the same id and call this function to grab an array of the objects you want.  This is not the most ideal way and its actually a pretty big hack.  But sometimes, speed is more important than form.</p>
]]></content:encoded>
			<wfw:commentRss>http://cmunezero.com/2008/03/17/javascript-tip-getelementsbyid/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
