<?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>scriptNode &#187; forms</title>
	<atom:link href="http://scriptnode.com/tag/forms/feed/" rel="self" type="application/rss+xml" />
	<link>http://scriptnode.com</link>
	<description>Tips and tricks for web developers.</description>
	<lastBuildDate>Tue, 01 Sep 2009 18:46:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Dynamic Input</title>
		<link>http://scriptnode.com/article/dynamic-input/</link>
		<comments>http://scriptnode.com/article/dynamic-input/#comments</comments>
		<pubDate>Sun, 15 Jun 2008 09:04:58 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Novice]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[script-sunday]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=24</guid>
		<description><![CDATA[
Welcome to the first Script Sunday! Each Sunday, I&#8217;ll be posting a script to the site. Some will be useful, some will be (hopefully) interesting, and some will be just for fun.


This week I&#8217;ve provided a very simple script that I&#8217;ve used countless times. See the search form in the sidebar? The default text should [...]]]></description>
			<content:encoded><![CDATA[<p>
Welcome to the first <a href="/tag/script-sunday/">Script Sunday</a>! Each Sunday, I&#8217;ll be posting a script to the site. Some will be useful, some will be (hopefully) interesting, and some will be just for fun.
</p>
<p>
This week I&#8217;ve provided a very simple script that I&#8217;ve used countless times. See the search form in the sidebar? The default text should be &#8220;Search for&#8230;&#8221;, but when you focus on the input, the text goes away (and sometimes the style changes), allowing you to search without having to delete any text first. This is <a href="http://digg.com/">very</a> <a href="http://www.mozilla.org/">common</a>, <a href="http://en.wikipedia.org/wiki/Unobtrusive_JavaScript">unobtrusive</a> behavior that&#8217;s used so much because it&#8217;s convenient and intuitive. Now you can use it too! Here&#8217;s the code:
</p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
/**<br />
 * Provides the common interaction of a basic style and text, then changing when a user<br />
 * focuses on an input element<br />
 * @param {Object} ops A key/value pair of options, including: id, text, classBlur, classFocus<br />
 * @return {Boolean} True on success, false on failure<br />
 * @author Matt Hackett (scriptnode.com)<br />
 */<br />
dynamicInput = function(ops) {</p>
<p>	var el = document.getElementById(ops.id);</p>
<p>	if (!el) {<br />
		return false;<br />
	}</p>
<p>	if (el.value == &#8221;) {<br />
		el.value = ops.text;<br />
	}</p>
<p>	el.onblur = function() {</p>
<p>		if (el.value == &#8221;) {<br />
			el.value = ops.text;<br />
		}</p>
<p>		if (ops.classBlur) {<br />
			el.className = ops.classBlur;<br />
		}</p>
<p>	};</p>
<p>	el.onfocus = function() {</p>
<p>		if (el.value == ops.text) {<br />
			el.value = &#8221;;<br />
		}</p>
<p>		if (ops.classFocus) {<br />
			el.className = ops.classFocus;<br />
		}</p>
<p>	};</p>
<p>	return true;</p>
<p>};<br />
</textarea></p>
<p>
You may notice that this script attaches to the events directly, which will override any previously set events using the same method (not recommended). The reason I did it this way is to remove any dependency on a JavaScript library. You <em>could</em> alter the code very easily to use whatever library you fancy. For instance, if you&#8217;re a <a href="http://developer.yahoo.com/yui/">YUI</a> fan, instead of <code>el.onblur = function(){}</code>, try <code>YAHOO.util.Event.addListener(el, 'blur', function(){});</code>.
</p>
<p>
There&#8217;s only one argument (an object) to pass into <code>dynamicInput</code>. The keys for this object are as follows:
</p>
<ul class="params">
<li><code>id</code> &#8211; The id of the input element</li>
<li><code>text</code> &#8211; The default text to use (for example, &#8220;Search Now!&#8221;)</li>
<li><code>classFocus</code> &#8211; The <acronym title="Cascading Style Sheets">CSS</acronym> class to set on the input when the focus event is fired (optional)</li>
<li><code>classBlur</code> &#8211; The <acronym title="Cascading Style Sheets">CSS</acronym> class to set on the input when the blur event is fired (optional)</li>
</ul>
<p>
And here&#8217;s a quick example of how to use this script:
</p>
<p><textarea class="xhtml" cols="50" name="code" rows="10"><br />
&lt;input id=&#8221;example&#8221; value=&#8221;"/&gt;</p>
<p>&lt;script type=&#8221;text/javascript&#8221;&gt;<br />
dynamicInput({<br />
	id : &#8216;example&#8217;,<br />
	text : &#8216;Click here to search&#8217;<br />
});<br />
</textarea></p>
<p>
It&#8217;s as easy as that! Enjoy!
</p>
<ul class="download">
<li><a href="/assets/js/dynamic-input/dynamic-input.js">[Download dynamic-input.js]</a> (789 bytes)</li>
<li><a href="/assets/js/dynamic-input/dynamic-input-min.js">[Download dynamic-input-min.js]</a> (313 bytes; compressed)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/dynamic-input/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
