<?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>Komodo Media &#187; forms</title>
	<atom:link href="http://www.komodomedia.com/tags/forms/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.komodomedia.com</link>
	<description>blog, webdesign, illustration &#38; newsk00l javascript</description>
	<lastBuildDate>Wed, 18 Jan 2012 14:43:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using jQuery to save form details</title>
		<link>http://www.komodomedia.com/blog/2008/07/using-jquery-to-save-form-details/</link>
		<comments>http://www.komodomedia.com/blog/2008/07/using-jquery-to-save-form-details/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 23:08:34 +0000</pubDate>
		<dc:creator>Rogie</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Nerdlab]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.komodomedia.com/?p=259</guid>
		<description><![CDATA[Ok, so I have a few loves in my life.  In web technologies, my love rests in design, jQuery programming and JavaScript, CSS and XHTML.  I love playing around with interfaces and front-end UI design and website design.  Today, I&#8217;m gonna bust out a short post on how I used a snippet [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, so I have a few loves in my life.  In web technologies, my love rests in design, jQuery programming and JavaScript, CSS and XHTML.  I love playing around with interfaces and front-end UI design and website design.  Today, I&#8217;m gonna bust out a short post on how I used a snippet of jQuery code to remember my user data, i.e. Name, website, email address, etc. to help out your users a wee bit.</p>
<h3>First, an example</h3>
<p>Let&#8217;s create a text input that we want to be remembered.  I&#8217;ll give the the every-so-appropriate name of <em>fav_starbucks_drink</em> Here it is:</p>
<p><label for="fav_starbucks_drink"><strong>Fav Starbucks drink o&#8217; choice:</strong> </label></p>
<input id="fav_starbucks_drink" name="fav_starbucks_drink" type="text" style="width:200px"/>
<script type="text/javascript">
<!--
$(document).ready( function(){remember("[name=fav_starbucks_drink]");} );
//--></script></p>
<p>For all you n00bs out there, the XHTML code is:</p>
<p><code>&lt;input name="fav_starbucks_drink" type="text" /&gt;</code></p>
<p>Test this jQuery code out by entering in a value in the input and then <a href="http://www.komodomedia.com/blog/2008/07/using-jquery-to-save-form-details/">refreshing the page</a>.</p>
<h3>jQuery Code:</h3>
<p>Realize two things. You&#8217;ll need the <a href="http://plugins.jquery.com/project/cookie">jQuery cookie plugin</a> as well as a <a href="http://docs.jquery.com/Downloading_jQuery">link to jQuery itself.</a> We are going to be using the cookie plugin to cookie or save the user&#8217;s data.</p>
<p><code>function remember( selector ){<br />
    $(selector).each(<br />
    	function(){<br />
    		//if this item has been cookied, restore it<br />
    		var name = $(this).attr('name');<br />
    		if( $.cookie( name ) ){<br />
    			$(this).val( $.cookie(name) );<br />
    		}<br />
    		//assign a change function to the item to cookie it<br />
    		$(this).change(<br />
    			function(){<br />
    				$.cookie(name, $(this).val(), { path: '/', expires: 365 });<br />
    			}<br />
    		);<br />
    	}<br />
    );<br />
}</code></p>
<h3>Restoring saved field values</h3>
<p>Aight. Let&#8217;s just dive into explaining this hunk of code.  First, you will notice the function declaration:<br />
<code>function remember( selector ){</code></p>
<p>The function parameter <em>selector</em> is a jQuery selector, which happens to be either a CSS3 selector that targets the desired inputs or a collection of jQuery objects.</p>
<p>Next:</p>
<p><code> $(selector).each( function{...</code></p>
<p><em>$.each</em> is a sweet function that executes the function passed in as it&#8217;s argument for every object within the jQuery collection as created by the <em>selector</em> parameter. If you are lost at this point, let it suffice to say that we are going to loop through all of the input items as identified by their selectors.</p>
<p>Now, lets digest a bigger chunk.  This next segment of code will perform the function of restoring any saved values.  Now, hopefully this script is being ran on page load, so this restore segment will restore the field values on page load.  Here&#8217;s that jazzy code now:</p>
<p><code>var name = $(this).attr('name');<br />
if( $.cookie( name ) ){<br />
    	$(this).val( $.cookie(name) );<br />
}</code></p>
<p>First, a variable <em>name</em> is created and set to the attribute of name on the input item.  What does that mean you ask?  Well, basically we don&#8217;t know the selector that targeted this input, but we want to store the name of the form item as we aren&#8217;t  guaranteed we will have an id. Cool now the name attribute of the input is stored in the var of name, so fittingly named.</p>
<p>Next, the code is <code class="inline">if( $.cookie( name ) )</code>. What&#8217;s that mean?  Think of it like this: &#8220;Is there a cookie with this name&#8221;? Okay, so if we have cookied the value for this input, it&#8217;ll be in that cookie. Radness. Okay, if there is a cookie, set this input item&#8217;s value attribute to that value.  Simple eh?  By using the <em>$.val()</em> function, I can set the value if I pass a parameter or I can get a value by leaving params emtpy. Eazy peezy &#8211; set the value to the cookie with that name:</p>
<p><code>$(this).val( $.cookie(name) );</code></p>
<p>Ok, now we have code for all input items of type text and textareas to be set.  We won&#8217;t cover checkboxes, radio&#8217;s and select lists on this post.</p>
<h3>Creating functions to save field values on change</h3>
<p>This block of code is uber simple. Check it:</p>
<p><code>$(this).change(<br />
    	function(){<br />
    	    	$.cookie(name, $(this).val(), { path: '/', expires: 365 });<br />
    	}<br />
);</code></p>
<p>Guess what <code class="inline">$(this).change()</code> does?  That&#8217;s right, it creates an onchange function handler for the targeted input item. So whatever function we pass the change function will be ran when input item&#8217;s value is changed. Word.</p>
<p>Wow, this one is easy.  Guess what, when this input item gets changed, cookie it.  That&#8217;s it.  In plain english, we are naming the cookie the name of the input item, we are assigning it the value of the input, it&#8217;s path is the root of the site and it expires 1 year from now. Rocking.</p>
<p>Last, lets run the code.  Do that by calling the function, passing it the selector to target the item.  Something like this will suffice:</p>
<p><code>remember( '[name=fav_starbucks_drink]' );</code></p>
<p>Ooh, check that out, <a href="http://www.w3.org/TR/REC-CSS2/selector.html#attribute-selectors">I used an attribute selector</a>, which since jQuery supports up to CSS3 selectors, will actually work on IE6. ;) </p>
<p>You could also run the code as soon as the document is ready by doing something like this:</p>
<p><code>$(document).ready(<br />
    function(){<br />
    	remember( '[name=fav_starbucks_drink]' );<br />
    }<br />
);</code></p>
<p>That&#8217;s it.  This code could obviously be improved no doubt, including handlers for select, checkboxes, radios and more, but you get the gist.  This script was uber easy to bust out.  I heart jQuery. Do you?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.komodomedia.com/blog/2008/07/using-jquery-to-save-form-details/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
	</channel>
</rss>

<!-- Served from: komodomedia.com @ 2012-02-10 13:40:52 by W3 Total Cache -->
