<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Prototype JavaScript framework - Utility Methods</title>
  <id>tag:prototypejs.org.,2008:mephisto/api/utility</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://prototypejs.org./feed/api/utility/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://prototypejs.org./api/utility" rel="alternate" type="text/html"/>
  <updated>2007-11-06T13:20:28Z</updated>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org.,2007-01-15:13069</id>
    <published>2007-01-15T23:15:00Z</published>
    <updated>2007-11-06T13:20:28Z</updated>
    <category term="Utility Methods"/>
    <category term="deprecated"/>
    <link href="http://prototypejs.org./api/utility/getElementsByClassName" rel="alternate" type="text/html"/>
    <title>document.getElementsByClassName</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;&quot;&gt;document.getElementsByClassName(className[, element]) -&gt; [HTMLElement...]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Retrieves (and extends) all the elements that have a CSS class name of &lt;code&gt;className&lt;/code&gt;.  The optional &lt;code&gt;element&lt;/code&gt; parameter specifies a parent element to search under.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;&quot;&gt;document.getElementsByClassName(className[, element]) -&gt; [HTMLElement...]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Retrieves (and extends) all the elements that have a CSS class name of &lt;code&gt;className&lt;/code&gt;.  The optional &lt;code&gt;element&lt;/code&gt; parameter specifies a parent element to search under.&lt;/p&gt;
&lt;p class=&quot;deprecated&quot;&gt;As of Prototype 1.6, &lt;code&gt;document.getElementsByClassName&lt;/code&gt; has been deprecated since native implementations return a &lt;code&gt;NodeList&lt;/code&gt; rather than an &lt;code&gt;Array&lt;/code&gt;. Please use &lt;a href=&quot;/api/utility/dollar-dollar&quot;&gt;&lt;code&gt;$$&lt;/code&gt;&lt;/a&gt; or &lt;a href=&quot;/api/element/select&quot;&gt;&lt;code&gt;Element#select&lt;/code&gt;&lt;/a&gt; instead.&lt;/p&gt;

&lt;p&gt;Note that each returned element has been &lt;a href=&quot;/api/element/extend&quot;&gt;extended&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Example&lt;/h3&gt;

&lt;h4&gt;HTML&lt;/h4&gt;

&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;
  &amp;lt;body&amp;gt;
    &amp;lt;div id=&amp;quot;one&amp;quot; class=&amp;quot;foo&amp;quot;&amp;gt;Single class name&amp;lt;/div&amp;gt;
    &amp;lt;div id=&amp;quot;two&amp;quot; class=&amp;quot;foo bar thud&amp;quot;&amp;gt;Multiple class names&amp;lt;/div&amp;gt;
    &amp;lt;ul id=&amp;quot;list&amp;quot;&amp;gt;
      &amp;lt;li id=&amp;quot;item_one&amp;quot; class=&amp;quot;thud&amp;quot;&amp;gt;List item 1&amp;lt;/li&amp;gt;
      &amp;lt;li&amp;gt;List item 2&amp;lt;/li&amp;gt;
      &amp;lt;li id=&amp;quot;item_two&amp;quot; class=&amp;quot;thud&amp;quot;&amp;gt;List item 3&amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
  &amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h4&gt;JavaScript&lt;/h4&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
  document.getElementsByClassName('foo');
  // -&gt; [HTMLElement, HTMLElement] (div#one, div#two)

  document.getElementsByClassName('thud');
  // -&gt; [HTMLElement, HTMLElement, HTMLElement] (div#two, li#item_one, li#item_two);

  document.getElementsByClassName('thud', $('list'));
  // -&gt; [HTMLElement, HTMLElement] (li#item_one, li#item_two)
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:prototypejs.org.,2006-12-07:12782</id>
    <published>2006-12-07T11:54:00Z</published>
    <updated>2006-12-13T14:46:10Z</updated>
    <category term="Utility Methods"/>
    <link href="http://prototypejs.org./api/utility/dollar-w" rel="alternate" type="text/html"/>
    <title>$w</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;$w(String) -&gt; Array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Splits a string into an &lt;code&gt;Array&lt;/code&gt;, treating all whitespace as delimiters. Equivalent to Ruby's &lt;code&gt;%w{foo bar}&lt;/code&gt; or Perl's &lt;code&gt;qw(foo bar)&lt;/code&gt;.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;$w(String) -&gt; Array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Splits a string into an &lt;code&gt;Array&lt;/code&gt;, treating all whitespace as delimiters. Equivalent to Ruby's &lt;code&gt;%w{foo bar}&lt;/code&gt; or Perl's &lt;code&gt;qw(foo bar)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This is one of those life-savers for people who just hate commas in literal arrays :-)&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;$w('apples bananas kiwis')
// -&gt; ['apples', 'bananas', 'kiwis']&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This can slightly shorten code when writing simple iterations:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;$w('apples bananas kiwis').each(function(fruit){
  var message = 'I like ' + fruit
  // do something with the message
})&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This also becomes sweet when combined with &lt;a href=&quot;/api/element&quot;&gt;&lt;code&gt;Element&lt;/code&gt;&lt;/a&gt; functions:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;$w('ads navbar funkyLinks').each(Element.hide);&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:prototypejs.org.,2006-12-06:12764</id>
    <published>2006-12-06T18:27:00Z</published>
    <updated>2006-12-13T14:47:23Z</updated>
    <category term="Utility Methods"/>
    <link href="http://prototypejs.org./api/utility/try-these" rel="alternate" type="text/html"/>
    <title>Try.these</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Try.these(Function...) -&gt; firstOKResult&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Accepts an arbitrary number of functions and returns the result of the first one that doesn't throw an error.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Try.these(Function...) -&gt; firstOKResult&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Accepts an arbitrary number of functions and returns the result of the first one that doesn't throw an error.&lt;/p&gt;
&lt;p&gt;This method provides a simple idiom for trying out blocks of code in sequence.  Such a sequence of attempts usually represents a downgrading approach to obtaining a given feature.&lt;/p&gt;

&lt;p&gt;In this example from Prototype's &lt;a href=&quot;/api/ajax&quot;&gt;&lt;code&gt;Ajax&lt;/code&gt;&lt;/a&gt; library, we want to get an &lt;code&gt;XMLHttpRequest&lt;/code&gt; object. Internet Explorer 6 and earlier, however, does not provide it as a vanilla JavaScript object, and will throw an error if we attempt a simple instantiation.  Also, over time, its proprietary way evolved, changing COM interface names.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Try.these&lt;/code&gt; will try several ways in sequence, from the best (and, theoretically, most widespread) one to the oldest and rarest way, returning the result of the first successful function.&lt;/p&gt;

&lt;p&gt;If none of the blocks succeeded, &lt;code&gt;Try.these&lt;/code&gt; will return &lt;code&gt;undefined&lt;/code&gt;, which will cause the &lt;code&gt;getTransport&lt;/code&gt; method in the example below to return &lt;code&gt;false&lt;/code&gt;, provided as a fallback result value.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;getTransport: function() {
  return Try.these(
    function() { return new XMLHttpRequest() },
    function() { return new ActiveXObject('Msxml2.XMLHTTP') },
    function() { return new ActiveXObject('Microsoft.XMLHTTP') }
  ) || false;
}&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Justin</name>
    </author>
    <id>tag:prototypejs.org.,2006-12-05:12740</id>
    <published>2006-12-05T21:37:00Z</published>
    <updated>2007-01-19T05:54:42Z</updated>
    <category term="Utility Methods"/>
    <link href="http://prototypejs.org./api/utility" rel="alternate" type="text/html"/>
    <title>Utility Methods</title>
<content type="html">
            &lt;p&gt;Prototype provides a number of &#8220;convenience&#8221; methods. Most are aliases of other Prototype methods, with the exception of the &lt;code&gt;$&lt;/code&gt; method, which wraps DOM nodes with additional functionality.&lt;/p&gt;

&lt;p&gt;These utility methods all address scripting needs that are &lt;strong&gt;so common&lt;/strong&gt; that their names were made as concise as can be.  Hence the &lt;code&gt;$&lt;/code&gt;-based convention.&lt;/p&gt;

&lt;p&gt;The most commonly used utility method is without doubt &lt;a href=&quot;/api/utility/dollar&quot;&gt;&lt;code&gt;$()&lt;/code&gt;&lt;/a&gt;, which is, for instance, used pervasively within Prototype&#8217;s code to let you pass either element IDs or actual DOM element references just about anywhere an element argument is possible. It actually goes &lt;strong&gt;way beyond&lt;/strong&gt; a simple wrapper around &lt;code&gt;document.getElementById&lt;/code&gt;; check it out to see just how useful it is.&lt;/p&gt;

&lt;p&gt;These methods are one of the cornerstones of efficient Prototype-based JavaScript coding. Take the time to learn them well.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org.,2006-10-28:12317</id>
    <published>2006-10-28T20:32:00Z</published>
    <updated>2007-01-21T22:20:43Z</updated>
    <category term="Utility Methods"/>
    <link href="http://prototypejs.org./api/utility/dollar-h" rel="alternate" type="text/html"/>
    <title>$H</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;$H([obj]) -&gt; Hash&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Creates a &lt;a href=&quot;/api/hash&quot;&gt;&lt;code&gt;Hash&lt;/code&gt;&lt;/a&gt; (which is synonymous to &#8220;map&#8221; or &#8220;associative array&#8221; for our purposes). A convenience wrapper around the &lt;code&gt;Hash&lt;/code&gt; constructor, with a safeguard that lets you pass an existing &lt;code&gt;Hash&lt;/code&gt; object and get it back untouched (instead of uselessly cloning it).&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;$H([obj]) -&gt; Hash&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Creates a &lt;a href=&quot;/api/hash&quot;&gt;&lt;code&gt;Hash&lt;/code&gt;&lt;/a&gt; (which is synonymous to &#8220;map&#8221; or &#8220;associative array&#8221; for our purposes). A convenience wrapper around the &lt;code&gt;Hash&lt;/code&gt; constructor, with a safeguard that lets you pass an existing &lt;code&gt;Hash&lt;/code&gt; object and get it back untouched (instead of uselessly cloning it).&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;$H&lt;/code&gt; function is the shorter way to obtain a hash (prior to 1.5 final, it was the &lt;em&gt;only&lt;/em&gt; proper way of getting one).&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org.,2006-10-28:12318</id>
    <published>2006-10-28T20:32:00Z</published>
    <updated>2007-03-12T00:24:00Z</updated>
    <category term="Utility Methods"/>
    <link href="http://prototypejs.org./api/utility/dollar-r" rel="alternate" type="text/html"/>
    <title>$R</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;$R(start, end[, exclusive = false]) -&gt; ObjectRange&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Creates a new &lt;code&gt;ObjectRange&lt;/code&gt; object. This method is a convenience wrapper around the &lt;a href=&quot;/api/objectRange&quot;&gt;&lt;code&gt;ObjectRange&lt;/code&gt;&lt;/a&gt; constructor, but &lt;code&gt;$R&lt;/code&gt; is the preferred alias.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;$R(start, end[, exclusive = false]) -&gt; ObjectRange&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Creates a new &lt;code&gt;ObjectRange&lt;/code&gt; object. This method is a convenience wrapper around the &lt;a href=&quot;/api/objectRange&quot;&gt;&lt;code&gt;ObjectRange&lt;/code&gt;&lt;/a&gt; constructor, but &lt;code&gt;$R&lt;/code&gt; is the preferred alias.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/api/objectRange&quot;&gt;&lt;code&gt;ObjectRange&lt;/code&gt;&lt;/a&gt; instances represent a range of consecutive values, be they numerical, textual, or of another type that semantically supports value ranges. See the type&#8217;s documentation for further details, and to discover how your own objects can support value ranges.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;$R&lt;/code&gt; function takes exactly the same arguments as the original constructor: the &lt;strong&gt;lower and upper bounds&lt;/strong&gt; (value of the same, proper type), and &lt;strong&gt;whether the upper bound is exclusive&lt;/strong&gt; or not. By default, the upper bound is inclusive.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
$R(0, 10).include(10)
// -&gt; true

$A($R(0, 5)).join(', ')
// -&gt; '0, 1, 2, 3, 4, 5'

$A($R('aa', 'ah')).join(', ')
// -&gt; 'aa, ab, ac, ad, ae, af, ag, ah'

$R(0, 10, true).include(10)
// -&gt; false

$R(0, 10, true).each(function(value) {
  // invoked 10 times for value = 0 to 9
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that &lt;code&gt;ObjectRange&lt;/code&gt; mixes in the &lt;a href=&quot;/api/enumerable&quot;&gt;&lt;code&gt;Enumerable&lt;/code&gt;&lt;/a&gt; module: this makes it easy to convert a range to an &lt;code&gt;Array&lt;/code&gt; (&lt;code&gt;Enumerable&lt;/code&gt; provides the &lt;a href=&quot;/api/enumerable/toArray&quot;&gt;&lt;code&gt;toArray&lt;/code&gt;&lt;/a&gt; method, which makes the &lt;a href=&quot;dollar-a&quot;&gt;&lt;code&gt;$A&lt;/code&gt;&lt;/a&gt; conversion straightforward), or to iterate through values. (Note, however, that getting the bounds back will be more efficiently done using the &lt;code&gt;start&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt; properties than calling the &lt;a href=&quot;/api/enumerable/min&quot;&gt;&lt;code&gt;min()&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;/api/enumerable/max&quot;&gt;&lt;code&gt;max()&lt;/code&gt;&lt;/a&gt; methods).&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org.,2006-10-28:12316</id>
    <published>2006-10-28T20:31:00Z</published>
    <updated>2007-01-15T19:57:29Z</updated>
    <category term="Utility Methods"/>
    <link href="http://prototypejs.org./api/utility/dollar-a" rel="alternate" type="text/html"/>
    <title>$A</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;$A(iterable) -&gt; actualArray&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Accepts an array-like collection (anything with numeric indices) and returns its equivalent as an actual &lt;code&gt;Array&lt;/code&gt; object. This method is a convenience alias of &lt;a href=&quot;/api/array/from&quot;&gt;&lt;code&gt;Array.from&lt;/code&gt;&lt;/a&gt;, but is the preferred way of casting to an &lt;code&gt;Array&lt;/code&gt;.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;$A(iterable) -&gt; actualArray&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Accepts an array-like collection (anything with numeric indices) and returns its equivalent as an actual &lt;code&gt;Array&lt;/code&gt; object. This method is a convenience alias of &lt;a href=&quot;/api/array/from&quot;&gt;&lt;code&gt;Array.from&lt;/code&gt;&lt;/a&gt;, but is the preferred way of casting to an &lt;code&gt;Array&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The primary use of &lt;code&gt;$A()&lt;/code&gt; is to obtain an actual &lt;code&gt;Array&lt;/code&gt; object based on anything that could pass as an array (e.g. the &lt;code&gt;NodeList&lt;/code&gt; or &lt;code&gt;HTMLCollection&lt;/code&gt; objects returned by numerous DOM methods, or the predefined &lt;code&gt;arguments&lt;/code&gt; reference within your functions).&lt;/p&gt;

&lt;p&gt;The reason you would want an actual &lt;code&gt;Array&lt;/code&gt; is simple: &lt;a href=&quot;/api/array&quot;&gt;Prototype extends &lt;code&gt;Array&lt;/code&gt;&lt;/a&gt; to equip it with numerous extra methods, and also mixes in the &lt;a href=&quot;/api/enumerable&quot;&gt;&lt;code&gt;Enumerable&lt;/code&gt;&lt;/a&gt; module, which brings in another boatload of nifty methods. Therefore, in Prototype, actual &lt;code&gt;Array&lt;/code&gt;s trump any other collection type you might otherwise get.&lt;/p&gt;

&lt;p&gt;The conversion performed is rather simple: &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; become an empty array; any object featuring an explicit &lt;code&gt;toArray&lt;/code&gt; method (as many Prototype objects do) has it invoked; otherwise, we assume the argument &quot;looks like an array&quot; (e.g. features a &lt;code&gt;length&lt;/code&gt; property and the &lt;code&gt;[]&lt;/code&gt; operator), and iterate over its components in the usual way.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;p&gt;The well-known DOM method &lt;a href=&quot;http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-A6C9094&quot;&gt;&lt;code&gt;document.getElementsByTagName()&lt;/code&gt;&lt;/a&gt; doesn't return an &lt;code&gt;Array&lt;/code&gt;, but a &lt;code&gt;NodeList&lt;/code&gt; object that implements the basic array &quot;interface.&quot; Internet Explorer does not allow us to extend &lt;code&gt;Enumerable&lt;/code&gt; onto &lt;code&gt;NodeList.prototype&lt;/code&gt;, so instead we cast the returned &lt;code&gt;NodeList&lt;/code&gt; to an &lt;code&gt;Array&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var paras = $A(document.getElementsByTagName('p'));
paras.each(Element.hide);
$(paras.last()).show();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice we had to use &lt;code&gt;each&lt;/code&gt; and &lt;code&gt;Element.hide&lt;/code&gt; because &lt;code&gt;$A&lt;/code&gt; doesn't perform DOM extensions, since the array could contain anything (not just DOM elements). To use the &lt;code&gt;hide&lt;/code&gt; instance method we first must make sure all the target elements are extended:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
$A(document.getElementsByTagName('p')).map(Element.extend).invoke('hide');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Want to display your arguments easily? &lt;code&gt;Array&lt;/code&gt; features a &lt;code&gt;join&lt;/code&gt; method, but the &lt;code&gt;arguments&lt;/code&gt; value that exists in all functions &lt;em&gt;does not&lt;/em&gt; inherit from &lt;code&gt;Array&lt;/code&gt;. So, the tough way, or the easy way?&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
// The hard way...
function showArgs() {
  alert(Array.prototype.join.call(arguments, ', '));
}

// The easy way...
function showArgs() {
  alert($A(arguments).join(', '));
}
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org.,2006-10-28:12315</id>
    <published>2006-10-28T20:29:00Z</published>
    <updated>2007-01-08T17:58:30Z</updated>
    <category term="Utility Methods"/>
    <link href="http://prototypejs.org./api/utility/dollar-f" rel="alternate" type="text/html"/>
    <title>$F</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;$F(element) -&gt; value&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the value of a form control. This is a convenience alias of &lt;a href=&quot;/api/form/element/getValue&quot;&gt;&lt;code&gt;Form.Element.getValue&lt;/code&gt;&lt;/a&gt;. Refer to it for full details.&lt;/p&gt;</summary>  </entry>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org.,2006-10-28:12314</id>
    <published>2006-10-28T20:28:00Z</published>
    <updated>2007-04-23T01:59:24Z</updated>
    <category term="Utility Methods"/>
    <link href="http://prototypejs.org./api/utility/dollar-dollar" rel="alternate" type="text/html"/>
    <title>$$</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;$$(cssRule...) -&gt; [HTMLElement...]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Takes an arbitrary number of CSS selectors (strings) and returns a document-order array of extended DOM elements that match any of them.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;$$(cssRule...) -&gt; [HTMLElement...]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Takes an arbitrary number of CSS selectors (strings) and returns a document-order array of extended DOM elements that match any of them.&lt;/p&gt;
&lt;p&gt;Sometimes the usual tools from your DOM arsenal -- &lt;code&gt;document.getElementById()&lt;/code&gt; encapsulated by &lt;a href=&quot;dollar&quot;&gt;&lt;code&gt;$()&lt;/code&gt;&lt;/a&gt;, &lt;code&gt;getElementsByTagName()&lt;/code&gt; and even Prototype's very own &lt;code&gt;getElementsByClassName()&lt;/code&gt; extensions -- just aren't enough to quickly find our elements or collections of elements. If you know the DOM tree structure, you can simply resort to CSS selectors to get the job done.&lt;/p&gt;

&lt;h3&gt;Quick examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;$$('div');
// -&gt; all DIVs in the document.  Same as document.getElementsByTagName('div')!

$$('#contents');
// -&gt; same as $('contents'), only it returns an array anyway.

$$('li.faux');
// -&gt; all LI elements with class 'faux'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;$$&lt;/code&gt; function searches the entire document.  For selector queries on more specific sections of a document, use &lt;a href=&quot;/api/element/methods/getElementsBySelector&quot;&gt;&lt;code&gt;Element#getElementsBySelector&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Supported CSS syntax&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;$$&lt;/code&gt; function does not rely on the browser's internal CSS parsing capabilities (otherwise, we'd be in cross-browser trouble...), and therefore offers a consistent set of selectors across all supported browsers.&lt;/p&gt;

&lt;h4&gt;supported in v1.5.0&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Type selector: tag names, as in &lt;code&gt;div&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Descendant selector: the space(s) between other selectors, as in &lt;code&gt;#a li&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Attribute selectors: the full CSS 2.1 set of &lt;code&gt;[attr]&lt;/code&gt;, &lt;code&gt;[attr=value]&lt;/code&gt;, &lt;code&gt;[attr~=value]&lt;/code&gt; and &lt;code&gt;[attr|=value]&lt;/code&gt;. It also supports &lt;code&gt;[attr!=value]&lt;/code&gt;. If the value you're matching against includes a space, be sure to enclose the value in quotation marks (&lt;code&gt;[title=&quot;Hello World!&quot;]&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Class selector: CSS class names, as in &lt;code&gt;.highlighted&lt;/code&gt; or &lt;code&gt;.example.wrong&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;ID selector: as in &lt;code&gt;#item1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;supported from v1.5.1&lt;/h4&gt;

&lt;p&gt;Virtually all of &lt;a href=&quot;http://www.w3.org/TR/2001/CR-css3-selectors-20011113/#selectors&quot;&gt;CSS3&lt;/a&gt; is supported, with the exception of pseudo-elements (like &lt;code&gt;::first-letter&lt;/code&gt;) and some pseudo-classes (like &lt;code&gt;:hover&lt;/code&gt;).  Some examples of new selectors that can be used in 1.5.1:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Child selector: selects immediate descendants, as in &lt;code&gt;#a &amp;gt; li&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Attribute selectors: all attribute operators are supported, including &lt;code&gt;~=&lt;/code&gt; (matches part of a space-delimited attribute value, like &lt;code&gt;rel&lt;/code&gt; or &lt;code&gt;class&lt;/code&gt;); &lt;code&gt;^=&lt;/code&gt; (matches the beginning of a value); &lt;code&gt;$=&lt;/code&gt; (matches the end of a value); and &lt;code&gt;*=&lt;/code&gt; (matches any part of the value).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;:not&lt;/code&gt; pseudo-class, as in &lt;code&gt;#a *:not(li)&lt;/code&gt; (matches all descendants of &lt;code&gt;#a&lt;/code&gt; that aren't LIs).&lt;/li&gt;
&lt;li&gt;All the &lt;code&gt;:nth&lt;/code&gt;, &lt;code&gt;:first&lt;/code&gt;, and &lt;code&gt;:last&lt;/code&gt; pseudo-classes.  Examples include &lt;code&gt;tr:nth-child(even)&lt;/code&gt; (all even table rows), &lt;code&gt;li:first-child&lt;/code&gt; (the first item in any list), or &lt;code&gt;p:nth-last-of-type(3)&lt;/code&gt; (the third-to-last paragraph on the page).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;:empty&lt;/code&gt; pseudo-class (for selecting elements without children or text content).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;:enabled&lt;/code&gt;, &lt;code&gt;:disabled&lt;/code&gt;, and &lt;code&gt;:checked&lt;/code&gt; pseudo-classes (for use with form controls).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;$$('#contents a[rel]');
// -&gt; all links inside the element of ID &quot;contents&quot; with a rel attribute

$$('a[href=&quot;#&quot;]');
// -&gt; all links with a href attribute of value &quot;#&quot; (eyeew!)

$$('#navbar a', '#sidebar a');
// -&gt; all links within the elements of ID &quot;navbar&quot; or &quot;sidebar&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;With version 1.5.1 and above&lt;/strong&gt; you can do various types of advanced selectors:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;$$('a:not([rel~=nofollow])');
// -&gt; all links, excluding those whose rel attribute contains the word &quot;nofollow&quot;

$$('table tbody &gt; tr:nth-child(even)');
// -&gt; all even rows within all table bodies

$$('div:empty');
// -&gt; all DIVs without content (i.e., whitespace-only)
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org.,2006-10-28:12313</id>
    <published>2006-10-28T20:23:00Z</published>
    <updated>2007-01-19T05:58:50Z</updated>
    <category term="Utility Methods"/>
    <link href="http://prototypejs.org./api/utility/dollar" rel="alternate" type="text/html"/>
    <title>$</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;$(id | element) -&gt; HTMLElement
$((id | element)...) -&gt; [HTMLElement...]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If provided with a string, returns the element in the document with matching ID; otherwise returns the passed element. Takes in an arbitrary number of arguments. All elements returned by the function are extended with Prototype DOM extensions.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;$(id | element) -&gt; HTMLElement
$((id | element)...) -&gt; [HTMLElement...]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If provided with a string, returns the element in the document with matching ID; otherwise returns the passed element. Takes in an arbitrary number of arguments. All elements returned by the function are extended with Prototype DOM extensions.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;$&lt;/code&gt; function is the cornerstone of Prototype, its Swiss Army knife.  Not only does it provide a handy alias for &lt;code&gt;document.getElementById&lt;/code&gt;, it also lets you pass indifferently IDs (strings) or DOM node references to your functions:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
function foo(element) {
    element = $(element);
    /*  rest of the function... */
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Code written this way is flexible — you can pass it the ID of the element or the element itself without any type sniffing.&lt;/p&gt;

&lt;p&gt;Invoking it with only one argument returns the element, while invoking it with multiple arguments returns an array of elements (and this works recursively: if you're twisted, you could pass it an array containing some arrays, and so forth).  As this is dependent on &lt;code&gt;getElementById&lt;/code&gt;, &lt;a href=&quot;http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-getElBId&quot;&gt;W3C specs&lt;/a&gt; apply: nonexistent IDs will yield &lt;code&gt;null&lt;/code&gt; and IDs present multiple times in the DOM will yield erratic results.  &lt;strong&gt;If you're assigning the same ID to multiple elements, you're doing it wrong!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The function also &lt;strong&gt;extends every returned element&lt;/strong&gt; with &lt;a href=&quot;/api/element/extend&quot;&gt;&lt;code&gt;Element.extend&lt;/code&gt;&lt;/a&gt; so you can use Prototype's DOM extensions on it. In the following code, the two lines are equivalent.  However, the second one feels significantly more object-oriented:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
// Note quite OOP-like...
Element.hide('itemId');
// A cleaner feel, thanks to guaranted extension
$('itemId').hide();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;However, when using iterators, leveraging the &lt;code&gt;$&lt;/code&gt; function makes for more elegant, more concise, and also more efficient code:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
['item1', 'item2', 'item3'].each(Element.hide);
// The better way:
$('item1', 'item2', 'item3').invoke('hide');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;See &lt;a href=&quot;/learn/extensions&quot;&gt;How Prototype extends the DOM&lt;/a&gt; for more info.&lt;/p&gt;
          </content>  </entry>
</feed>
