<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lasse Bunk&#039;s weblog &#187; Prototype</title>
	<atom:link href="http://lassebunk.dk/tag/prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://lassebunk.dk</link>
	<description>Ruby on Rails, programming, and SEO</description>
	<lastBuildDate>Sat, 19 May 2012 10:18:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Default input values using labels and Prototype</title>
		<link>http://lassebunk.dk/2009/07/15/default-input-values-using-labels-and-prototype/</link>
		<comments>http://lassebunk.dk/2009/07/15/default-input-values-using-labels-and-prototype/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 20:45:03 +0000</pubDate>
		<dc:creator>lassebunk</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[Default values]]></category>
		<category><![CDATA[Form]]></category>
		<category><![CDATA[Input]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Prototype]]></category>

		<guid isPermaLink="false">http://lassebunk.dk/?p=63</guid>
		<description><![CDATA[How to set and retrieve default input values using the Prototype javascript framework.]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> For jQuery, check out the <a href="https://github.com/danawoodman/jQuery-Input-Replacement-Plugin">jQuery Input Replacement plugin</a>.</p>
<p>So, maybe you&#8217;ve seen this one too many times:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;input type=&quot;text&quot; id=&quot;username&quot; name=&quot;username&quot; value=&quot;Enter username&quot;
       onfocus=&quot;if (this.value == 'Enter username') this.value = '';&quot;
       onblur=&quot;if (this.value == '') this.value = 'Enter username';&quot; /&gt;
</pre>
<p>A lot of code duplication there.</p>
<p>What if you could do it with labels and without writing a single line of Javascript? Like this:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;label for=&quot;username&quot; class=&quot;default-value&quot;&gt;Enter username&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;username&quot; name=&quot;username&quot;&gt;
</pre>
<p>I wanted to do a default value replacement that was easy and intuitive to use and at the same time being semantically correct. As an example, a non-semantic way could be to add a &#8220;default-value&#8221; attribute to the input.</p>
<p>I would also like it to show the default values as labels if the browser has either CSS or Javascript disabled.</p>
<p>As shown above, we can do this by adding a label for each input, giving the labels a class called &#8220;default-value&#8221;. The concept is then to use <a href="http://www.prototypejs.org">Prototype</a> to iterate through the labels and use their values as default for the associated inputs.</p>
<p>When the input is in &#8220;default mode&#8221; it should have a class called &#8220;default&#8221; so we can use this to gray out the text like this:</p>
<pre class="brush: css; title: ; notranslate">
.default {
	color: #999;
}
</pre>
<p>We go through each label like this:</p>
<pre class="brush: jscript; title: ; notranslate">
$$(&quot;label.default-value&quot;).each(function(label) {
  // code goes here
});
</pre>
<p>For each label we set up some variables:</p>
<pre class="brush: jscript; title: ; notranslate">

/* Change this to your default class name */
var defaultClass = &quot;default&quot;;

/* Get the default value */
var defaultValue = label.innerHTML;

/* Get the associated input */
var input = $(label.htmlFor);

/* Get the form */
var form = input.form;

/* Store information about the input being a password so we can use this later */
var isPassword = (input.type &amp;&amp; input.type == &quot;password&quot;);
</pre>
<p>Then add a focus observer:</p>
<pre class="brush: jscript; title: ; notranslate">
/* When input gets focus, set value to &quot;&quot; if value is default value */
input.observe(&quot;focus&quot;, function(event) {
	if (input.value == defaultValue) {
		input.value = &quot;&quot;;
		input.removeClassName(defaultClass);
		if (isPassword) input.type = &quot;password&quot;;
	}
});
</pre>
<p>A blur observer:</p>
<pre class="brush: jscript; title: ; notranslate">
/* When input loses focus, set value to default value if value = &quot;&quot; */
input.observe(&quot;blur&quot;, function(event) {
	if (input.value == &quot;&quot;) {
		input.value = defaultValue;
		input.addClassName(defaultClass);
		if (isPassword) input.type = &quot;text&quot;;
	}
});
</pre>
<p>Observe when the form is submitted:</p>
<pre class="brush: jscript; title: ; notranslate">
/* Set default values to &quot;&quot; when form is submitted */
if (form) {
	form.observe(&quot;submit&quot;, function(event) {
		if (input.value == defaultValue) {
			input.value = &quot;&quot;;
		}
	});
}
</pre>
<p>Set the default values:</p>
<pre class="brush: jscript; title: ; notranslate">
/* Set default value when page loads */
if (input.value == &quot;&quot; || input.value == defaultValue) {
	input.value = defaultValue;
	input.addClassName(defaultClass);
	if (isPassword) input.type = &quot;text&quot;;
}
</pre>
<p>And finally hide the label:</p>
<pre class="brush: jscript; title: ; notranslate">
/* Hide the label using javascript so css-but-no-javascript browsers will see the label */
label.hide();
</pre>
<p>That&#8217;s it.</p>
<p>Now you can set up your form like this:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;script src=&quot;prototype.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;default-values.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
...
&lt;style type=&quot;text/css&quot;&gt;
	.default {
		color: #999;
	}
&lt;/style&gt;
...
&lt;form&gt;
	&lt;label for=&quot;username&quot; class=&quot;default-value&quot;&gt;Enter username&lt;/label&gt;
	&lt;input type=&quot;text&quot; id=&quot;username&quot; name=&quot;username&quot;&gt;
&lt;/form&gt;
</pre>
<p>&#8230; and have your values replaced automatigally <img src='http://lassebunk.dk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Demo: <a href="http://lassebunk.dk/demos/default-values/default-values.html">See a demo</a><br />
Download: <a href="http://lassebunk.dk/demos/default-values/default-values.js">default-values.js</a>, <a href="http://www.prototypejs.org/download">prototype.js</a></p>
<p>
<a href="http://twitter.com/lassebunk" onclick="abtest.trackGoal(21, this);" class="icon twitter">Follow me on Twitter</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://lassebunk.dk/2009/07/15/default-input-values-using-labels-and-prototype/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

