<?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>SoftLayer Blog &#187; data attributes</title>
	<atom:link href="http://blog.softlayer.com/tag/data-attributes/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.softlayer.com</link>
	<description>A Behind the Scenes Look at the Best Hosting Provider in the World</description>
	<lastBuildDate>Wed, 15 May 2013 15:33:34 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>Web Development &#8211; HTML5 &#8211; Custom Data Attributes</title>
		<link>http://blog.softlayer.com/2012/web-development-html5-custom-data-attributes/</link>
		<comments>http://blog.softlayer.com/2012/web-development-html5-custom-data-attributes/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 17:30:29 +0000</pubDate>
		<dc:creator>Philip Thompson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[SoftLayer]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[data attributes]]></category>
		<category><![CDATA[examples]]></category>
		<category><![CDATA[form validation]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[speed]]></category>
		<category><![CDATA[update]]></category>
		<category><![CDATA[walkthrough]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=6843</guid>
		<description><![CDATA[I recently worked on a project that involved creating promotion codes for our clients. I wanted to make this tool as simple as possible to use and because this involved dealing with thousands of our products in dozens of categories with custom pricing for each of these products, I had to find a generic way [...]]]></description>
			<content:encoded><![CDATA[<p>I recently worked on a project that involved creating promotion codes for our clients. I wanted to make this tool as simple as possible to use and because this involved dealing with thousands of our products in dozens of categories with custom pricing for each of these products, I had to find a generic way to deal with client-side form validation. I didn&#8217;t want to write custom JavaScript functions for each of the required inputs, so I decided to use custom data attributes.</p>
<p>Last month, we started a series focusing on web development tips and tricks with a post about <a href="http://blog.softlayer.com/2011/html5-javascript-web-development-part-i/">JavaScript optimization</a>. In this installment, we&#8217;re cover how to use HTML5 custom data attributes to assist you in validating forms.</p>
<p><a href="http://www.w3.org/TR/html5/elements.html#custom-data-attribute">Custom data attributes</a> for elements are &#8220;[attributes] in no namespace whose name starts with the string &#8216;data-&#8217;, has at least one character after the hyphen, is XML-compatible, and contains no characters in the range U+0041 to U+005A (LATIN CAPITAL LETTER A to LATIN CAPITAL LETTER Z).&#8221; Thanks W3C. That definition is bookish, so let&#8217;s break it down and look at some examples.</p>
<style type="text/css">
code{color:blue;}
</style>
<p style="margin-bottom:0; padding-bottom:0;"><strong>Valid:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div data<span style="color: #339933;">-</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Philip&quot;</span><span style="color: #339933;">&gt;</span>Mr<span style="color: #339933;">.</span> Thompson is an okay guy<span style="color: #339933;">.&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;softlayer.com&quot;</span> data<span style="color: #339933;">-</span>company<span style="color: #339933;">-</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SoftLayer&quot;</span> data<span style="color: #339933;">-</span>company<span style="color: #339933;">-</span>state<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;TX&quot;</span><span style="color: #339933;">&gt;</span>SoftLayer<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>li data<span style="color: #339933;">-</span>color<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;blue&quot;</span><span style="color: #339933;">&gt;</span>Smurfs<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span></pre></div></div>

<p style="margin-bottom:0; padding-bottom:0;"><strong>Invalid:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// This attribute is not prefixed with 'data-'</span>
    <span style="color: #339933;">&lt;</span>h2 database<span style="color: #339933;">-</span>id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;244&quot;</span><span style="color: #339933;">&gt;</span>Food<span style="color: #339933;">&lt;/</span>h2<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// These 2 attributes contain capital letters in the attribute names</span>
    <span style="color: #339933;">&lt;</span>p data<span style="color: #339933;">-</span>firstName<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Ashley&quot;</span> data<span style="color: #339933;">-</span>lastName<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Thompson&quot;</span><span style="color: #339933;">&gt;...&lt;/</span>p<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// This attribute does not have any valid characters following 'data-'</span>
    <span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/images/pizza.png&quot;</span> data<span style="color: #339933;">-=</span><span style="color: #0000ff;">&quot;Sausage&quot;</span> <span style="color: #339933;">/&gt;</span></pre></div></div>

<p style="margin-bottom:0;padding-bottom:0;">Now that you know what custom data attributes are, why would we use them? Custom attributes allow us to relate specific information to particular elements. This information is hidden to the end user, so we don&#8217;t have to worry about the data cluttering screen space and we don&#8217;t have to create separate hidden elements whose purpose is to hold custom data (which is just bad practice). This data can be used by a JavaScript programmer to many different ends. Among the most common use cases are to manipulate elements, provide custom styles (using CSS) and perform form validation. In this post, we&#8217;ll focus on form validation. </p>
<p><span id="more-6843"></span></p>
<p>Let&#8217;s start out with a simple form with two radio inputs. Since I like food, our labels will be food items!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;radio&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;food&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pizza&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>required<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;food&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>show<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;.pizza&quot;</span> <span style="color: #339933;">/&gt;</span> Pizza
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;radio&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;food&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sandwich&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>required<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;food&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>show<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#sandwich&quot;</span> <span style="color: #339933;">/&gt;</span> Sandwich
<span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden required&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>error<span style="color: #339933;">-</span>food<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>A food item must be selected<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Here we have two standard radio inputs with some custom data attributes and a hidden div (using CSS) that contains our error message. The first input has a name of <code>food</code> and a value of <code>pizza</code> &ndash; these will be used on the server side once our form is submitted. There are two custom data attributes for this input: <code>data-sl-required</code> and <code>data-sl-show</code>. I am defining the data attribute <code>data-sl-required</code> to indicate that this radio button is required in the form and one of them must be selected to pass validation. Note that I have prefixed <code>required</code> with <code>sl-</code> to <a href="http://en.wikipedia.org/wiki/Namespace">namespace</a> my data attribute. <code>required</code> is generic and I don&#8217;t want to have any conflicts with any other attributes – especially ones written for other projects! The value of <code>data-sl-required</code> is <code>food</code>, which I have tied to the div with the attribute <code>data-sl-error-food</code> (more on this later). </p>
<p>The second custom attribute is <code>data-sl-show</code> with a selector of <code>.pizza</code>. (To review selectors, jump back to the <a href="http://blog.softlayer.com/2011/html5-javascript-web-development-part-i/">JavaScript optimization</a> post.) This data attribute will be used to show a hidden div that contains the class <code>pizza</code> when the radio button is clicked. The <code>sandwich</code> radio input has the same data attributes but with slightly different values.</p>
<p style="margin-bottom:0;padding-bottom:0;">Now we can move on to our HTML with the hidden text inputs:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden pizza&quot;</span><span style="color: #339933;">&gt;</span>
    Pizza type<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pizza&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>required<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pizza&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>depends<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{&amp;quot;type&amp;quot;:&amp;quot;radio&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;food&amp;quot;,&amp;quot;value&amp;quot;:&amp;quot;sandwich&amp;quot;}&quot;</span> <span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden required&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>error<span style="color: #339933;">-</span>pizza<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>The pizza type must not be empty<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sandwich&quot;</span><span style="color: #339933;">&gt;</span>
    Sandwich<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sandwich&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>required<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sandwich&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>depends<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{&amp;quot;type&amp;quot;:&amp;quot;radio&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;food&amp;quot;,&amp;quot;value&amp;quot;:&amp;quot;sandwich&amp;quot;}&quot;</span> <span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden required&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>error<span style="color: #339933;">-</span>sandwich<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>The sandwich must not be empty<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<p style="margin-bottom:0;padding-bottom:0;">These are hidden divs that contain text inputs that will be used to input more data depending on the radio input selected. Notice that the first outer div has a class of <code>pizza</code> and the second outer div has an id of <code>sandwich</code>. These values tie back to the <code>data-sl-show</code> selectors for the radio inputs. These text inputs also contain the <code>data-sl-required</code> attribute like the previous radio inputs. The <code>data-sl-depends</code> data attribute is a fun attribute that contains a JSON-encoded object that&#8217;s been run through PHP&#8217;s <em>htmlentities()</em> function. For readability, the <code>data-sl-depends</code> values contain:</p>
<div style="margin-left:20px;margin-top:0;"><code>{"type":"radio","name":"food","value":"pizza"}</code><br />
<code>{"type":"radio","name":"food","value":"sandwich"}</code></div>
<p>This first attribute says that our text input “depends” on the <code>radio</code> input with the name <code>food</code> and value <code>pizza</code> to be visible and selected in order to be processed as required. You can just imagine the possibilities of combinations you can create to make very custom functionality with very generic JavaScript.</p>
<p>Now we can examine our JavaScript to make sense of all these custom data attributes. Note that I&#8217;ll be using Mootools&#8217; syntax, but the same can fairly easily be accomplished using jQuery or your favorite JavaScript framework. I&#8217;m going to start by creating a <em>DataForm</em> class. This will be generic enough so that you can use it in multiple forms and it&#8217;s not tied to this specific instance. Reuse is good! To have it fit our needs, we&#8217;re going to pass some options when instantiating it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">new</span> DataForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    ...<span style="color: #339933;">,</span>
    dataAttributes<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        required<span style="color: #339933;">:</span> <span style="color: #3366CC;">'data-sl-required'</span><span style="color: #339933;">,</span>
        errorPrefix<span style="color: #339933;">:</span> <span style="color: #3366CC;">'data-sl-error'</span><span style="color: #339933;">,</span>
        depends<span style="color: #339933;">:</span> <span style="color: #3366CC;">'data-sl-depends'</span><span style="color: #339933;">,</span>
        show<span style="color: #339933;">:</span> <span style="color: #3366CC;">'data-sl-show'</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, I&#8217;m using the data attribute names from our form as the options – this will allow you to create your own data attribute names depending on your uses. We first need to make our hidden divs visible whenever our radio buttons are clicked – we&#8217;ll use our <em>initData()</em> method for that.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">initData<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> attrs <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">dataAttributes</span><span style="color: #339933;">,</span>
        divs <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    $$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[type=radio]['</span><span style="color: #339933;">+</span>attrs.<span style="color: #660066;">show</span><span style="color: #339933;">+</span><span style="color: #3366CC;">']'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> div <span style="color: #339933;">=</span> $$<span style="color: #009900;">&#40;</span>input.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>attrs.<span style="color: #660066;">show</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        divs.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>div<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        input.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'change'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            divs.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>div<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> div.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            div.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This method grabs all the radio inputs with the <em>show</em> attribute (<code>data-sl-show</code>) and adds an <em>onchange</em> event to each of them. When a radio input is checked, it first hides all the divs, and then shows the div that&#8217;s associated with that radio input.</p>
<p>Great! Now we have our text inputs showing and hiding depending on which radio button is selected. Now onto the actual validation. First, we&#8217;ll make sure our required radio inputs are checked:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[type=radio]['</span><span style="color: #339933;">+</span>attrs.<span style="color: #660066;">required</span><span style="color: #339933;">+</span><span style="color: #3366CC;">']:not(:checked)'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #339933;">=</span> input.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    checkError<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> isRadioWithNameSelected<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This grabs all the unchecked radio inputs with the <em>required</em> attribute (<code>data-sl-required</code>) and checks to see if any radio with that same name is selected using the function <em>isRadioWithNameSelected()</em>. The <em>checkError()</em> function will show or hide the error div (<code>data-sl-error-*</code>) depending if the radio is checked or not. Don&#8217;t worry &#8211; you&#8217;ll see how these functions are implemented in the JSFiddle below.</p>
<p>Now we can check our text inputs:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[type=text]['</span><span style="color: #339933;">+</span>attrs.<span style="color: #660066;">required</span><span style="color: #339933;">+</span><span style="color: #3366CC;">']:visible'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #339933;">=</span> input.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        depends <span style="color: #339933;">=</span> input.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>attrs.<span style="color: #660066;">depends</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        value <span style="color: #339933;">=</span> input.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'value'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>depends<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        depends <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">encode</span><span style="color: #009900;">&#40;</span>depends<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>depends.<span style="color: #660066;">type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'radio'</span><span style="color: #339933;">:</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>depends.<span style="color: #000066;">name</span> <span style="color: #339933;">&amp;&amp;</span> depends.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> radio <span style="color: #339933;">=</span> $$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[type=radio][name=&quot;'</span><span style="color: #339933;">+</span>depends.<span style="color: #000066;">name</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;][value=&quot;'</span><span style="color: #339933;">+</span>depends.<span style="color: #660066;">value</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;]:checked'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>radio<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        checkError<span style="color: #009900;">&#40;</span>input.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>attrs.<span style="color: #660066;">required</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    checkError<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> value<span style="color: #339933;">!=</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This obtains the required and visible text inputs and determines if they are empty or not. Here we look at our <code>depends</code> object to grab the associated radio inputs. If that radio is checked and the text input is empty, the error message appears. If that radio is not checked, it doesn&#8217;t even evaluate that text input. The <code>depends.type</code> is in a <em>switch</em> statement to allow for easy expansion of types. There are other cases for evaluating relationships &#8230; I&#8217;ll let you come up with more for yourself.</p>
<p>That concludes our usage of custom data attributes in form validation. This really only touched upon the very tip of the iceberg. Data attributes allow you &ndash; the creative developer &ndash; to interact in a new, HTML-valid way with your web pages.</p>
<p>Check out the working example of the above code at <a href="http://jsfiddle.net/philthathril/YyKbt/1/">jsfiddle.net</a>. To read more on custom data attributes, see what Google has to say. If you want to see really cool functionality that uses data attributes plus so much more, check out Aaron Newton&#8217;s <a href="http://www.clientcide.com/code-releases/clientcide-3-0-behavior-1-0-mootools-development-and-so-much-more/">Behavior module</a> over at <a href="http://www.clientcide.com/">clientcide.com</a>.</p>
<p>Happy coding!</p>
<p>-Philip</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2012/web-development-html5-custom-data-attributes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
