<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Prototype JavaScript framework - Form.Element</title>
  <id>tag:www.prototypejs.org,2009:mephisto/api/form/element</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://www.prototypejs.org/feed/api/form/element/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://www.prototypejs.org/api/form/element" rel="alternate" type="text/html"/>
  <updated>2007-01-15T20:19:37Z</updated>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:www.prototypejs.org,2006-12-09:12814</id>
    <published>2006-12-09T16:55:00Z</published>
    <updated>2007-01-15T20:19:37Z</updated>
    <category term="Form.Element"/>
    <link href="http://www.prototypejs.org/api/form/element/enable" rel="alternate" type="text/html"/>
    <title>enable</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;enable(element) -&gt; HTMLElement&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Enables a previously disabled form control.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;enable(element) -&gt; HTMLElement&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Enables a previously disabled form control.&lt;/p&gt;
&lt;h3&gt;Example&lt;/h3&gt;

&lt;p&gt;See the interactive example in the &lt;a href=&quot;../disable&quot;&gt;Form.disable()&lt;/a&gt; method, which is basically it.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:www.prototypejs.org,2006-12-09:12813</id>
    <published>2006-12-09T16:48:00Z</published>
    <updated>2007-01-15T20:19:33Z</updated>
    <category term="Form.Element"/>
    <link href="http://www.prototypejs.org/api/form/element/disable" rel="alternate" type="text/html"/>
    <title>disable</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;disable(element) -&gt; HTMLElement&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Disables a form control, effectively preventing its value to be changed until it is enabled again.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;disable(element) -&gt; HTMLElement&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Disables a form control, effectively preventing its value to be changed until it is enabled again.&lt;/p&gt;
&lt;p&gt;This method sets the native &lt;code&gt;disabled&lt;/code&gt; property of an element to &lt;code&gt;true&lt;/code&gt;. You can use this property to check the state of a control.&lt;/p&gt;

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

&lt;p&gt;See the interactive example in the &lt;a href=&quot;../disable&quot;&gt;Form.disable()&lt;/a&gt; method, which is basically it.&lt;/p&gt;

&lt;h3&gt;Notes&lt;/h3&gt;

&lt;p&gt;Disabled form controls are never serialized.&lt;/p&gt;

&lt;p&gt;Never disable a form control as a security measure without having validation for it server-side. A user with minimal experience of JavaScript can enable these fields on your site easily using any browser. Instead, use disabling as a usability enhancement - with it you can indicate that a specific value should not be changed at the time being.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:www.prototypejs.org,2006-12-09:12812</id>
    <published>2006-12-09T16:47:00Z</published>
    <updated>2007-01-15T18:09:36Z</updated>
    <category term="Form.Element"/>
    <link href="http://www.prototypejs.org/api/form/element/clear" rel="alternate" type="text/html"/>
    <title>clear</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;clear(element) -&gt; HTMLElement&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Clears the contents of a text input.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;clear(element) -&gt; HTMLElement&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Clears the contents of a text input.&lt;/p&gt;
&lt;h3&gt;Example&lt;/h3&gt;

&lt;p&gt;This code sets up a text field in a way that it clears its contents the first time it receives focus:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
$('some_field').onfocus = function() {
  // if already cleared, do nothing
  if (this._cleared) return

  // when this code is executed, &quot;this&quot; keyword will in fact be the field itself
  this.clear()
  this._cleared = true
}
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:www.prototypejs.org,2006-12-09:12811</id>
    <published>2006-12-09T16:46:00Z</published>
    <updated>2007-05-16T13:38:18Z</updated>
    <category term="Form.Element"/>
    <link href="http://www.prototypejs.org/api/form/element/getValue" rel="alternate" type="text/html"/>
    <title>getValue</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;getValue(element) -&gt; string | array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the current value of a form control. A string is returned for most controls; only multiple select boxes return an array of values. The global shortcut for this method is &lt;a href=&quot;/api/utility/dollar-f&quot;&gt;&lt;code&gt;$F()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;getValue(element) -&gt; string | array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the current value of a form control. A string is returned for most controls; only multiple select boxes return an array of values. The global shortcut for this method is &lt;a href=&quot;/api/utility/dollar-f&quot;&gt;&lt;code&gt;$F()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;How to reference form controls by their &lt;em&gt;name&lt;/em&gt;&lt;/h3&gt;

&lt;p&gt;This method is consistent with other DOM extensions in that it requires an element &lt;strong&gt;ID&lt;/strong&gt; as the string argument, not the name of the form control (as some might think). If you want to reference controls by their names, first find the control the regular JavaScript way and use the node itself instead of an ID as the argument.&lt;/p&gt;

&lt;p&gt;For example, if you have an input named &quot;company&quot; in a form with an ID &quot;contact&quot;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;var form = $('contact');
var input = form['company'];

Form.Element.getValue(input);

// but, the preferred call is:
$(input).getValue(); // we used the $() method so the node gets extended

// you can also use the shortcut
$F(input);
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Note&lt;/h3&gt;

&lt;p&gt;An error is thrown (&quot;element has no properties&quot;) if the &lt;code&gt;element&lt;/code&gt; argument is an unknown ID.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:www.prototypejs.org,2006-12-09:12810</id>
    <published>2006-12-09T16:45:00Z</published>
    <updated>2006-12-09T16:46:05Z</updated>
    <category term="Form.Element"/>
    <link href="http://www.prototypejs.org/api/form/element/serialize" rel="alternate" type="text/html"/>
    <title>serialize</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;serialize(element) -&gt; string&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Creates an URL-encoded string representation of a form control in the &lt;code&gt;name=value&lt;/code&gt; format.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;serialize(element) -&gt; string&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Creates an URL-encoded string representation of a form control in the &lt;code&gt;name=value&lt;/code&gt; format.&lt;/p&gt;
&lt;p&gt;The result of this method is a string suitable for Ajax requests. However, it serializes only a single element - if you need to serialize the whole form use &lt;a href=&quot;../serialize&quot;&gt;&lt;code&gt;Form.serialize()&lt;/code&gt;&lt;/a&gt; instead.&lt;/p&gt;

&lt;h3&gt;Notes&lt;/h3&gt;

&lt;p&gt;Serializing a disabled control or a one without a name will always result in an empty string.&lt;/p&gt;

&lt;p&gt;If you simply need an element's value for reasons other than Ajax requests, use &lt;a href=&quot;getValue&quot;&gt;&lt;code&gt;getValue()&lt;/code&gt;&lt;/a&gt; instead.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:www.prototypejs.org,2006-12-09:12809</id>
    <published>2006-12-09T16:44:00Z</published>
    <updated>2007-02-10T17:38:35Z</updated>
    <category term="Form.Element"/>
    <link href="http://www.prototypejs.org/api/form/element/select" rel="alternate" type="text/html"/>
    <title>select</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;select(element) -&gt; HTMLElement&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Selects the current text in a text input.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;select(element) -&gt; HTMLElement&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Selects the current text in a text input.&lt;/p&gt;
&lt;h3&gt;Example&lt;/h3&gt;

&lt;p&gt;Some search boxes are set up so that they auto-select their content when they receive focus.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
$('searchbox').onfocus = function() {
  Form.Element.select(this)
  // You can also rely on the native method, but this will NOT return the element!
  this.select()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Focusing + selecting: use &lt;code&gt;activate&lt;/code&gt;!&lt;/h3&gt;

&lt;p&gt;The &lt;a href=&quot;/api/form/element/activate&quot;&gt;&lt;code&gt;activate&lt;/code&gt;&lt;/a&gt; method is a nifty way to both focus a form field and select its current text, all in one portable JavaScript call.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:www.prototypejs.org,2006-12-09:12808</id>
    <published>2006-12-09T16:40:00Z</published>
    <updated>2007-02-10T17:41:50Z</updated>
    <category term="Form.Element"/>
    <link href="http://www.prototypejs.org/api/form/element/focus" rel="alternate" type="text/html"/>
    <title>focus</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;focus(element) -&gt; HTMLElement&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Gives keyboard focus to an element.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;focus(element) -&gt; HTMLElement&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Gives keyboard focus to an element.&lt;/p&gt;
&lt;h3&gt;Example&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
Form.Element.focus('searchbox')
// Almost equivalent, but does NOT return the form element (uses the native focus() method):
$('searchbox').focus()
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:www.prototypejs.org,2006-12-09:12806</id>
    <published>2006-12-09T12:04:00Z</published>
    <updated>2007-01-18T16:43:59Z</updated>
    <category term="Form.Element"/>
    <link href="http://www.prototypejs.org/api/form/element/present" rel="alternate" type="text/html"/>
    <title>present</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;present(element) -&gt; boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns true if a text input has contents, false otherwise.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;present(element) -&gt; boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns true if a text input has contents, false otherwise.&lt;/p&gt;
&lt;h3&gt;Example&lt;/h3&gt;

&lt;p&gt;This method is very handy in a generic form validation routine. Try to submit the following form first without filling out the information, then again after typing some text in the fields:&lt;/p&gt;

&amp;lt;form class=&quot;example&quot; action=&quot;#&quot; id=&quot;example&quot;&gt;
  &lt;fieldset&gt;
    &lt;legend&gt;User Details&lt;/legend&gt;
    &lt;p class=&quot;message&quot;&gt;Please fill out the following fields:&lt;/p&gt;
    &lt;div&gt;
      &amp;lt;label for=&quot;username&quot;&gt;Username&amp;lt;/label&gt;
      &amp;lt;input name=&quot;username&quot; type=&quot;text&quot; id=&quot;username&quot; /&gt;
    &lt;/div&gt;
    &lt;div&gt;
      &amp;lt;label for=&quot;email&quot;&gt;Email Address&amp;lt;/label&gt;
      &amp;lt;input name=&quot;email&quot; type=&quot;text&quot; id=&quot;email&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&quot;buttonrow&quot;&gt;&amp;lt;input type=&quot;submit&quot; value=&quot;submit&quot; /&gt;&lt;/div&gt;
  &lt;/fieldset&gt;

  &lt;em&gt;all&lt;/em&gt; the fields.').style.color = 'red'
      }
      return false
    }
  &amp;lt;/form&gt;

&lt;p&gt;Here is the complete JavaScript code for the above example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
$('example').onsubmit = function(){
  var valid, msg = $('msg')

  // are both fields present?
  valid = $(this.username).present() &amp;&amp; $(this.email).present()

  if (valid) {
    // in real world we would return true here to allow the form to be submitted
    // return true
    msg.update('Passed validation!').style.color = 'green'
  } else {
    msg.update('Please fill out &lt;em&gt;all&lt;/em&gt; the fields.').style.color = 'red'
  }
  return false
}
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:www.prototypejs.org,2006-12-09:12805</id>
    <published>2006-12-09T12:02:00Z</published>
    <updated>2007-02-10T17:40:50Z</updated>
    <category term="Form.Element"/>
    <link href="http://www.prototypejs.org/api/form/element/activate" rel="alternate" type="text/html"/>
    <title>activate</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;activate(element) -&gt; HTMLElement&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Gives focus to a form control and selects its contents if it is a text input.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;activate(element) -&gt; HTMLElement&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Gives focus to a form control and selects its contents if it is a text input.&lt;/p&gt;
&lt;p&gt;This method is just a shortcut for focusing and selecting; therefore, these are equivalent (aside from the fact that the former one will &lt;strong&gt;not&lt;/strong&gt; return the field) :&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
Form.Element.focus('myelement').select()
$('myelement').activate()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Guess which call is the nicest? ;)&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Justin</name>
    </author>
    <id>tag:www.prototypejs.org,2006-12-05:12747</id>
    <published>2006-12-05T21:45:00Z</published>
    <updated>2007-01-16T16:53:35Z</updated>
    <category term="Form.Element"/>
    <link href="http://www.prototypejs.org/api/form/element" rel="alternate" type="text/html"/>
    <title>Form.Element</title>
<content type="html">
            &lt;p&gt;This is a collection of methods that assist in dealing with form controls. They provide ways to focus, serialize, disable/enable or extract current value from a specific control.&lt;/p&gt;

&lt;p&gt;In Prototype, &lt;code&gt;Form.Element&lt;/code&gt; is also aliased &lt;code&gt;Field&lt;/code&gt; and all these methods are available directly on INPUT, SELECT and TEXTAREA elements that have been extended (see &lt;a href=&quot;/learn/extensions&quot;&gt;&#8220;How Prototype extends the DOM&#8221;&lt;/a&gt;). Therefore, these are equivalent:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
Form.Element.activate('myfield')
Field.activate('myfield')
$('myfield').activate()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Naturally, you should always prefer the shortest form suitable in a situation. Most of these methods also return the element itself (as indicated by the return type) for chainability.&lt;/p&gt;
          </content>  </entry>
</feed>
