<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>DOM Scripting comments: getElementsByClass</title>
        <link>http://domscripting.com/blog/display/18</link>
        <description>The DOM Scripting equivalent of building a better mousetrap is the quest for a good getElementsByClass function.</description>
        <language>en</language>
        <item>
            <title>Scott</title>
            <link>http://domscripting.com/blog/display.php/18#comment54</link>
            <content:encoded><![CDATA[<p>Oh, I just learnt that you can do prototyping on DOM node constructors in Safari, so you don&#8217;t have to make getElementsByClass an object method (bad practise). Make sure you also add this to the Document prototype.</p>

<p>Document = document.constructor;
HTMLElement = document.createElement(&quot;x&quot;).constructor;</p>

<p>Document.prototype.getElementsByClassName = HTMLElement.prototype.getElementsByClassName = function(class) {</p>

<p>// you know what</p>

<p>}</p>
]]></content:encoded>
            <pubDate>Wed, 05 Oct 2005 12:37:12 GMT</pubDate>
        </item>
        <item>
            <title>Scott</title>
            <link>http://domscripting.com/blog/display.php/18#comment53</link>
            <content:encoded><![CDATA[<p>I find it easier to make getElementsByClassName an object method. This way I can use it just like getElementsByTagName, while also reducing the number of required arguments the function needs to be passed.</p>

<p>Object.prototype.getElementsByClassName = function(searchClass) {</p>

<p>var els = this.getElementsByTagName(&quot;*&quot;);</p>

<p>// rest of function</p>

<p>}</p>

<p>e.g. var african_elephants = document.getElementById(&quot;africa&quot;).getElementsByClassName(&quot;elephant&quot;);</p>
]]></content:encoded>
            <pubDate>Tue, 04 Oct 2005 05:53:58 GMT</pubDate>
        </item>
        <item>
            <title>Dustin Diaz</title>
            <link>http://domscripting.com/blog/display.php/18#comment52</link>
            <content:encoded><![CDATA[<p>Hey Jeremy, thanks for posting this. This is Dustin ( found you in my rerrers list :) ). That&#8217;s great feedback about the foo-bar Lachlan. I must have skipped that possibility.</p>

<p>I agree Jeremy that it is possibly a bit resource intensive&#8230; I originally thought of doing a .split(&#8217; &#8216;), but then you run a resource-intensive for loop nested in a for loop, which in general, I try to avoid loops nested in loops.</p>

<p>The cssQuery thing just came out like a week ago or so, so that&#8217;s a possible goodie as well.</p>
]]></content:encoded>
            <pubDate>Mon, 03 Oct 2005 17:42:26 GMT</pubDate>
        </item>
        <item>
            <title>peterned</title>
            <link>http://domscripting.com/blog/display.php/18#comment51</link>
            <content:encoded><![CDATA[<p>insertAfter would be the same as an insertBefore on the nextSibling, and if this sibling is non-existent, an appendChild seems to be performed automatically anyway. So, if more browsers would support it, adding insertAfter to a page would be very simple:</p>

<p>HTMLElement.prototype.insertAfter = function(element, insert) {
    insert.parentNode.insertBefore(element, insert.nextSibling);
}</p>

<p>For IE support you could make it a global function (or linked to &quot;document&quot;), since the parentNode you perform it on can also be retrieved from the insert.parentNode&#8230; It&#8217;s like you said, the gaps are very plugable.</p>
]]></content:encoded>
            <pubDate>Mon, 03 Oct 2005 08:35:24 GMT</pubDate>
        </item>
        <item>
            <title>Dominic Mitchell</title>
            <link>http://domscripting.com/blog/display.php/18#comment50</link>
            <content:encoded><![CDATA[<p>If you find yourself needing slightly more functionality than this, then Dean Edwards&#8217; cssQuery is very useful.  It lets you select elements in a similiar manner to getElementsByClass, except that instead of just class name, you can use <em>any</em> CSS selector.  More details at:</p>

<p><a href="http://dean.edwards.name/my/cssQuery/">http://dean.edwards.name/my/cssQuery/</a></p>
]]></content:encoded>
            <pubDate>Sun, 02 Oct 2005 10:02:26 GMT</pubDate>
        </item>
        <item>
            <title>Lachlan Hunt</title>
            <link>http://domscripting.com/blog/display.php/18#comment49</link>
            <content:encoded><![CDATA[<p>Oh, your comment system munged the code.  There&#8217;s supposed to be 2 slashes before the &#92;s, and I left out some quote marks.  Hopefully it works this time.</p>

<p>RegExp(&quot;(^|&#92;&#92;s)&quot; + className + &quot;(&#92;&#92;s|$)&quot;);</p>
]]></content:encoded>
            <pubDate>Sun, 02 Oct 2005 05:12:04 GMT</pubDate>
        </item>
        <item>
            <title>Lachlan Hunt</title>
            <link>http://domscripting.com/blog/display.php/18#comment48</link>
            <content:encoded><![CDATA[<p>That RegExp is buggy.  Try class=&quot;foo-bar&quot; and getElementsByClass(document,&quot;foo&quot;,*);</p>

<p>Use this instead:
var pattern = new RegExp((^|&#92;s)&quot; + searchClass + &quot;(&#92;s|$));</p>
]]></content:encoded>
            <pubDate>Sun, 02 Oct 2005 05:08:22 GMT</pubDate>
        </item>
        <item>
            <title>Lachlan Hunt</title>
            <link>http://domscripting.com/blog/display.php/18#comment47</link>
            <content:encoded><![CDATA[<p>There&#8217;s a getElementsByClassName() function defined in the WHATWG&#8217;s Web Apps 1 spec, which takes an arbitary number of arguments, each representing one class name.
<a href="http://www.whatwg.org/specs/web-apps/current-work/#getelementsbyclassname">http://www.whatwg.org/specs/web-apps/current-work/#getelementsbyclassname</a></p>

<p>I&#8217;ve already got a prototype implementation that, AFAIK, fully conforms to the spec.  I believe it&#8217;s still got some Safari bugs in it, but it works in Firefox, Opera and IE.
<a href="http://lachy.id.au/dev/script/examples/DOM/DOM.js">http://lachy.id.au/dev/script/examples/DOM/DOM.js</a></p>
]]></content:encoded>
            <pubDate>Sat, 01 Oct 2005 23:47:54 GMT</pubDate>
        </item>
   </channel>
</rss>