<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: javascript code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 21 Aug 2008 04:19:53 GMT</pubDate>
    <description>DZone Snippets: javascript code</description>
    <item>
      <title>Style a collection of elements quickly</title>
      <link>http://snippets.dzone.com/posts/show/1774</link>
      <description>Quickly set multiple style properties to single elements or arrays/nodeLists of elements.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function setStyles(el, css) {&lt;br /&gt;	var l = el.length;&lt;br /&gt;	if (typeof l == 'undefined') {el = [el]; l = 1;}&lt;br /&gt;	for (var i=0; i&lt;l; i++) {&lt;br /&gt;		for (var s in css) el[i].style[s] = css[s];&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Usage:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;window.onload = function() {&lt;br /&gt;	// single element&lt;br /&gt;	setStyles(document.body, {background:'yellow'});&lt;br /&gt;	// nodeList&lt;br /&gt;	setStyles(document.getElementsByTagName('div'), {&lt;br /&gt;		background: '#def',&lt;br /&gt;		color: 'red'&lt;br /&gt;	});&lt;br /&gt;	// array&lt;br /&gt;	var a = [document.body, document.getElementsByTagName('div')[0]];&lt;br /&gt;	setStyles(a, {border: '1px black dotted'});&lt;br /&gt;};&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 26 Mar 2006 07:24:30 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1774</guid>
      <author>mrclay (Steve Clay)</author>
    </item>
    <item>
      <title>Empty a DOM node</title>
      <link>http://snippets.dzone.com/posts/show/1757</link>
      <description>&lt;code&gt;&lt;br /&gt;while (node.hasChildNodes()) {node.removeChild(node.firstChild);}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 24 Mar 2006 21:09:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1757</guid>
      <author>mrclay (Steve Clay)</author>
    </item>
    <item>
      <title>Faster looping over DOMCollections</title>
      <link>http://snippets.dzone.com/posts/show/1756</link>
      <description>&lt;code&gt;&lt;br /&gt;var i=0, el;&lt;br /&gt;while (el = document.getElementsByTagName('div').item(i++)) {&lt;br /&gt;    // use el here&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Calling document.getElementsByTagName('div').item() directly saves the browser from having to return a whole DOMCollection. Using the while loop prevents the browser from ever having to calculate a DOMCollection's length, and gives us a syntactically nicer reference to the current node.&lt;br /&gt;&lt;br /&gt;Source: David "liorean" Andersson. Here's an e-mail excerpt:&lt;br /&gt;&lt;br /&gt;&gt; DOM implementations are optimised for single element access, and DOMCollections &lt;b&gt;[like document.getElementsByTagName()]&lt;/b&gt; are particularly badly optimised since they are dynamic, which means they need to be either polled at regular intervals, event controlled or recollected at each access. So, instead of caching the DOMCollection, you can make the script never have to create a DOMCollection at all. A perfomance win that will be considerable, and that can actually be felt if you're doing DHTML. ... &lt;b&gt;[calling item() directly, the browser]&lt;/b&gt; never has to create a dynamic collection, it just searches for the element with the given order, and discards the created list. Browsers do optimise for accessing collection members this way.</description>
      <pubDate>Fri, 24 Mar 2006 20:59:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1756</guid>
      <author>mrclay (Steve Clay)</author>
    </item>
  </channel>
</rss>
