<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: getElementsByClassName Deluxe Edition</title>
	<atom:link href="http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/feed/" rel="self" type="application/rss+xml" />
	<link>http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/</link>
	<description>the personal blog of Stuart Colville covering modern web development techniques and best practices</description>
	<pubDate>Thu, 20 Nov 2008 10:55:29 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Nick</title>
		<link>http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-64387</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Mon, 09 Jun 2008 11:53:37 +0000</pubDate>
		<guid isPermaLink="false">http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-64387</guid>
		<description>Nicely done!

In the innermost loop, on line 17 of the function, you could change "c == arrClass.length" to "c == n", since you already set n to the length of arrClass in the for statement.

In fact, you could simplify the compound if statement on line 17 by pre-computing the second operand of the comparison before the outermost loop (two lines added; three changed):

&lt;code&gt;
  var arrClass = strClass.split(delim);
  var n = arrClass.length;
  var d = (delim == '&#124;') ? 1 : n;
  for (var i = 0, j = objColl.length; i  arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (var k = 0, l = arrObjClass.length; k &#60; l; k++) {
      for (var m = 0; m &#60; n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if (c == d) {
          arr.push(objColl[i]);
          break comparisonLoop;
        }
      }
    }
  }
&lt;/code&gt;

Also, if the user of getElementsByClassName accidentally puts more than one space between class names, as in...

&lt;code&gt;var myObjColl = getElementsByClassName('one  two');&lt;/code&gt;

... arrClass will contain a zero-length string, and the function will return an array with lots of extraneous, empty elements!  Of course, you could argue that the user &lt;em&gt;shouldn't&lt;/em&gt; put more than one space between class names, but it wouldn't be too difficult to make the function allow for this.</description>
		<content:encoded><![CDATA[<p>Nicely done!</p>
<p>In the innermost loop, on line 17 of the function, you could change &#8220;c == arrClass.length&#8221; to &#8220;c == n&#8221;, since you already set n to the length of arrClass in the for statement.</p>
<p>In fact, you could simplify the compound if statement on line 17 by pre-computing the second operand of the comparison before the outermost loop (two lines added; three changed):</p>
<p><code><br />
  var arrClass = strClass.split(delim);<br />
  var n = arrClass.length;<br />
  var d = (delim == '|') ? 1 : n;<br />
  for (var i = 0, j = objColl.length; i  arrObjClass.length) continue;<br />
    var c = 0;<br />
    comparisonLoop:<br />
    for (var k = 0, l = arrObjClass.length; k &lt; l; k++) {<br />
      for (var m = 0; m &lt; n; m++) {<br />
        if (arrClass[m] == arrObjClass[k]) c++;<br />
        if (c == d) {<br />
          arr.push(objColl[i]);<br />
          break comparisonLoop;<br />
        }<br />
      }<br />
    }<br />
  }<br />
</code></p>
<p>Also, if the user of getElementsByClassName accidentally puts more than one space between class names, as in&#8230;</p>
<p><code>var myObjColl = getElementsByClassName('one  two');</code></p>
<p>&#8230; arrClass will contain a zero-length string, and the function will return an array with lots of extraneous, empty elements!  Of course, you could argue that the user <em>shouldn&#8217;t</em> put more than one space between class names, but it wouldn&#8217;t be too difficult to make the function allow for this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lessan</title>
		<link>http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-64002</link>
		<dc:creator>Lessan</dc:creator>
		<pubDate>Tue, 19 Feb 2008 07:07:14 +0000</pubDate>
		<guid isPermaLink="false">http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-64002</guid>
		<description>Thanks for the script - used it as a quick replacement for this function from the protoype library, in an old site. Works much faster now.</description>
		<content:encoded><![CDATA[<p>Thanks for the script - used it as a quick replacement for this function from the protoype library, in an old site. Works much faster now.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: brad</title>
		<link>http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-58067</link>
		<dc:creator>brad</dc:creator>
		<pubDate>Fri, 31 Aug 2007 14:17:44 +0000</pubDate>
		<guid isPermaLink="false">http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-58067</guid>
		<description>Hi !

Really great ! Thanks for this !</description>
		<content:encoded><![CDATA[<p>Hi !</p>
<p>Really great ! Thanks for this !</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: k-ny</title>
		<link>http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-37908</link>
		<dc:creator>k-ny</dc:creator>
		<pubDate>Fri, 11 May 2007 16:50:31 +0000</pubDate>
		<guid isPermaLink="false">http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-37908</guid>
		<description>Great tools !</description>
		<content:encoded><![CDATA[<p>Great tools !</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: riptide</title>
		<link>http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-33143</link>
		<dc:creator>riptide</dc:creator>
		<pubDate>Wed, 21 Mar 2007 21:09:03 +0000</pubDate>
		<guid isPermaLink="false">http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-33143</guid>
		<description>I can't understand I have the whole code in the head of my document but it dosent work.

var myObjColl = getElementsByClassName('tt1', 'a');
for (var i = 0, j = myObjColl.length; i </description>
		<content:encoded><![CDATA[<p>I can&#8217;t understand I have the whole code in the head of my document but it dosent work.</p>
<p>var myObjColl = getElementsByClassName(&#8217;tt1&#8242;, &#8216;a&#8217;);<br />
for (var i = 0, j = myObjColl.length; i</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stuart Colville</title>
		<link>http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-30862</link>
		<dc:creator>Stuart Colville</dc:creator>
		<pubDate>Mon, 12 Mar 2007 14:06:02 +0000</pubDate>
		<guid isPermaLink="false">http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-30862</guid>
		<description>@javasciptn00b: getElementsByClassname returns a collection, so to do what you want you would have to iterate over the collection in a loop and set the style property of each object in turn

&lt;pre&gt;&lt;code&gt;
var c = getElementsByClassName('blah');

for(var i=0;j=c.length; i&#60;j; i++){
  c[i].style.display = 'none';
}&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>@javasciptn00b: getElementsByClassname returns a collection, so to do what you want you would have to iterate over the collection in a loop and set the style property of each object in turn</p>
<pre><code>
var c = getElementsByClassName('blah');

for(var i=0;j=c.length; i&lt;j; i++){
  c[i].style.display = &#8216;none&#8217;;
}</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: javascriptn00b</title>
		<link>http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-30851</link>
		<dc:creator>javascriptn00b</dc:creator>
		<pubDate>Mon, 12 Mar 2007 09:15:46 +0000</pubDate>
		<guid isPermaLink="false">http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-30851</guid>
		<description>Looks really cool,

anyone know if it is possible to create something that does
document.getElementsByClass(class).style.display ='none';

basically the same as when using
document.getElementsById(id).style.display ='none';

it would be a great help, if this would be possible

Thx in advance lotz</description>
		<content:encoded><![CDATA[<p>Looks really cool,</p>
<p>anyone know if it is possible to create something that does<br />
document.getElementsByClass(class).style.display =&#8217;none&#8217;;</p>
<p>basically the same as when using<br />
document.getElementsById(id).style.display =&#8217;none&#8217;;</p>
<p>it would be a great help, if this would be possible</p>
<p>Thx in advance lotz</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stuart Colville</title>
		<link>http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-23593</link>
		<dc:creator>Stuart Colville</dc:creator>
		<pubDate>Thu, 25 Jan 2007 09:20:53 +0000</pubDate>
		<guid isPermaLink="false">http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-23593</guid>
		<description>@Gary: Unfortunately classes are always delimited by spaces so if you have a classname with a space in it you are going to run into problems across the board. To that end, I would suggest revising renaming that class to something more suitable.</description>
		<content:encoded><![CDATA[<p>@Gary: Unfortunately classes are always delimited by spaces so if you have a classname with a space in it you are going to run into problems across the board. To that end, I would suggest revising renaming that class to something more suitable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gary</title>
		<link>http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-23569</link>
		<dc:creator>Gary</dc:creator>
		<pubDate>Thu, 25 Jan 2007 01:22:00 +0000</pubDate>
		<guid isPermaLink="false">http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-23569</guid>
		<description>I like your script but because of one thing it's not suitable for my purpose.
&lt;pre&gt;&lt;code&gt;var cont = document.getElementById('container');
var myObjColl = getElementsByClassName('one two', 'a', cont);
for (var i = 0, j = myObjColl.length; i &lt;/code&gt;&lt;/pre&gt;
The argument 'one two' let you search for elements of class 'one' AND 'two' but I wanna search on a class name that has spaces in it.
My suggestion it to use something like 'one&#38;two' as what you did in the "OR" case.</description>
		<content:encoded><![CDATA[<p>I like your script but because of one thing it&#8217;s not suitable for my purpose.</p>
<pre><code>var cont = document.getElementById('container');
var myObjColl = getElementsByClassName('one two', 'a', cont);
for (var i = 0, j = myObjColl.length; i </code></pre>
<p>The argument &#8216;one two&#8217; let you search for elements of class &#8216;one&#8217; AND &#8216;two&#8217; but I wanna search on a class name that has spaces in it.<br />
My suggestion it to use something like &#8216;one&amp;two&#8217; as what you did in the &#8220;OR&#8221; case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gavy</title>
		<link>http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-23266</link>
		<dc:creator>Gavy</dc:creator>
		<pubDate>Thu, 18 Jan 2007 17:00:28 +0000</pubDate>
		<guid isPermaLink="false">http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/#comment-23266</guid>
		<description>Just want to say thanks for the example given. From your idea i have re-write my own function and that speeded up our application load time by almost 4s in a large dom! Great work!</description>
		<content:encoded><![CDATA[<p>Just want to say thanks for the example given. From your idea i have re-write my own function and that speeded up our application load time by almost 4s in a large dom! Great work!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
