<?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>Robertdot, A Web Design Blog &#187; How To</title>
	<atom:link href="http://robertdot.org/tags/how-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://robertdot.org</link>
	<description>A web design blog for designers that develop or developers that design.</description>
	<lastBuildDate>Fri, 05 Mar 2010 15:59:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Element Swapping with Unobtrusive JavaScript</title>
		<link>http://robertdot.org/2008/02/11/element-swapping-with-unobtrusive-javascript/</link>
		<comments>http://robertdot.org/2008/02/11/element-swapping-with-unobtrusive-javascript/#comments</comments>
		<pubDate>Mon, 11 Feb 2008 16:45:25 +0000</pubDate>
		<dc:creator>Robert</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://robertdot.org/2008/02/11/element-swapping-with-unobtrusive-javascript/</guid>
		<description><![CDATA[
 There may come a time when you need to show different content on one page based on a user-selected option.  Here&#8217;s a quick tutorial.


 The first step is to set up your content properly.  In this example, I&#8217;ll use a select to swap between two form elements, though the basic premise can [...]]]></description>
			<content:encoded><![CDATA[<p>
 There may come a time when you need to show different content on one page based on a user-selected option.  Here&#8217;s a quick tutorial.
</p>
<p>
 The first step is to set up your content properly.  In this example, I&#8217;ll use a <code>select</code> to swap between two form elements, though the basic premise can be applied to other design patters such as <a href="http://developer.yahoo.com/ypatterns/pattern.php?pattern=moduletabs">module tabs</a>.  Here is how the form should look.
</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="htmlstrict" style="font-family:monospace;">&lt;form method=&quot;post&quot; action=&quot;post.php&quot;&gt;
 &lt;div&gt;
  &lt;label&gt;
   How Should I Reply To You?
   &lt;select name=&quot;type&quot; id=&quot;type&quot;&gt;
    &lt;option value=&quot;email&quot;&gt;E-mail&lt;/option&gt;
    &lt;option value=&quot;phone&quot;&gt;Phone&lt;/option&gt;
   &lt;/select&gt;
  &lt;/label&gt;
 &lt;/div&gt;
 &lt;div id=&quot;byemail&quot;&gt;
  &lt;label&gt;
   Enter Your Email:
   &lt;input type=&quot;text&quot; name=&quot;email&quot;&gt;
  &lt;/label&gt;
 &lt;/div&gt;
 &lt;div id=&quot;byphone&quot;&gt;
  &lt;label&gt;
   Enter Your Phone Number:
   &lt;input type=&quot;text&quot; name=&quot;phone&quot;&gt;
  &lt;/label&gt;
 &lt;/div&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>
 That is the basic setup you need.  You have something for the user to select (that has an <code>id</code> to assign, in this case, an <code>onchange</code> event), and something to swap between that have <code>id</code>s.  Now you need some JavaScript.  Comments are inline to explain things.
</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Assign the onload event to the function init. */</span>
<span style="color: #009966; font-style: italic;">/* Test which way adding events is supported. */</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009966; font-style: italic;">/* W3C method. */</span>
	window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'load'</span><span style="color: #339933;">,</span> init<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009966; font-style: italic;">/* IE method. */</span>
	window.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onload'</span><span style="color: #339933;">,</span> init<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009966; font-style: italic;">/* Old school method. */</span>
	window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> init<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009966; font-style: italic;">/* This is the function that runs after the page loads. */</span>
<span style="color: #003366; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009966; font-style: italic;">/* Get the select element you want the onchange to be applied to. I use the j_ prefix because sometimes errors happen when variables are named the same thing as IDs in the HTML. */</span>
	<span style="color: #003366; font-weight: bold;">var</span> j_type <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'type'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009966; font-style: italic;">/* If the element exists, we can assign the onchange event to call the swap function using the same type test we used before. */</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>j_type<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>j_type.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			j_type.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'change'</span><span style="color: #339933;">,</span> swap<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>j_type.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			j_type.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onchange'</span><span style="color: #339933;">,</span> swap<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			j_type.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> swap<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #009966; font-style: italic;">/* Now, we need to fire the event to get the default. */</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">createEvent</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #009966; font-style: italic;">/* The W3C method. */</span>
			<span style="color: #003366; font-weight: bold;">var</span> j_event <span style="color: #339933;">=</span> document.<span style="color: #660066;">createEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'HTMLEvents'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			j_event.<span style="color: #660066;">initEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;change&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</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>
			j_type.<span style="color: #660066;">dispatchEvent</span><span style="color: #009900;">&#40;</span>j_event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">createEventObject</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #009966; font-style: italic;">/* The IE method. */</span>
			j_type.<span style="color: #660066;">fireEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;onchange&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009966; font-style: italic;">/* This is the function that does the work of swapping the elements. */</span>
<span style="color: #003366; font-weight: bold;">function</span> swap<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009966; font-style: italic;">/* Get both the elements you want to swap between. */</span>
	<span style="color: #003366; font-weight: bold;">var</span> j_email <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'byemail'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> j_phone <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'byphone'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009966; font-style: italic;">/* Grab the event from the W3C method or the IE method. */</span>
	<span style="color: #003366; font-weight: bold;">var</span> e <span style="color: #339933;">=</span> e <span style="color: #339933;">||</span> window.<span style="color: #660066;">srcEvent</span><span style="color: #339933;">;</span>
	<span style="color: #009966; font-style: italic;">/* Grab the event target from the W3C method or the IE method. */</span>
	<span style="color: #003366; font-weight: bold;">var</span> target <span style="color: #339933;">=</span> e.<span style="color: #660066;">target</span> <span style="color: #339933;">||</span> e.<span style="color: #660066;">srcElement</span><span style="color: #339933;">;</span>
	<span style="color: #009966; font-style: italic;">/* Grab the target's value */</span>
	<span style="color: #003366; font-weight: bold;">var</span> t_value <span style="color: #339933;">=</span> target.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
	<span style="color: #009966; font-style: italic;">/* If both swap elements exist... */</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>j_email <span style="color: #339933;">&amp;&amp;</span> j_phone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #009966; font-style: italic;">/* Depending on which option, hide or show the form elements. */</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>t_value <span style="color: #339933;">==</span> <span style="color: #3366CC;">'email'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			j_email.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'block'</span><span style="color: #339933;">;</span>
			j_phone.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>t_value <span style="color: #339933;">==</span> <span style="color: #3366CC;">'phone'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			j_email.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
			j_phone.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'block'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			j_email.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
			j_phone.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</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;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>
 Save the script above to a file and include it in the head of your document that the form is in.  You&#8217;ll have the swapping capabilities you desire using unobtrusive JavaScript and modern <abbr title="Document Object Model">DOM</abbr> techniques.
</p>
<p>
 This was tested in Safari 3, FireFox 2, Opera 9.25, and Internet Explorer 7.</p>
]]></content:encoded>
			<wfw:commentRss>http://robertdot.org/2008/02/11/element-swapping-with-unobtrusive-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easy Clearing Of Floats</title>
		<link>http://robertdot.org/2007/04/24/easy-clearing-of-floats/</link>
		<comments>http://robertdot.org/2007/04/24/easy-clearing-of-floats/#comments</comments>
		<pubDate>Tue, 24 Apr 2007 20:14:29 +0000</pubDate>
		<dc:creator>Robert</dc:creator>
				<category><![CDATA[Web Design]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://robertdot.org/2007/04/24/easy-clearing-of-floats/</guid>
		<description><![CDATA[
 Web standards designers often need to make use of display:float to handle things like columns, navigation lists, and more.  Usually these floats are followed by something like &#60;div style="clear:both"&#62;&#60;/div&#62;.  It turns out there is an easier way.


 I was reading 35 Designers x 5 Questions and saw a link for a way [...]]]></description>
			<content:encoded><![CDATA[<p>
 Web standards designers often need to make use of <code>display:float</code> to handle things like columns, navigation lists, and more.  Usually these floats are followed by something like <code>&lt;div style="clear:both"&gt;&lt;/div&gt;</code>.  It turns out there is an easier way.
</p>
<p>
 I was reading <a href="http://www.smashingmagazine.com/2007/04/20/35-designers-x-5-questions/">35 Designers x 5 Questions</a> and saw a link for a way to <a href="http://www.sitepoint.com/blogs/2005/02/26/simple-clearing-of-floats/">clear <code>float</code>s without <code>clear</code> <code>div</code>s</a>.  The code that comes with the article is a bit confusing and sloppily written, but it works!
</p>
<p>
 So, to make things easier to understand, I wrote my own example.  The code below will do it for you.
</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
</pre></td><td class="code"><pre class="htmlstrict" style="font-family:monospace;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
&lt;html&gt;
 &lt;head&gt;
  &lt;title&gt;Easy Floats&lt;/title&gt;
  &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html;charset=iso-8859-1&quot;&gt;
  &lt;style&gt;
h1 {
	background-color:#FFCCCC;
	margin:auto;
	width:500px;
	text-align:center;
	margin-bottom:0px;
}
div#outer{
	width:500px;
	overflow:auto;
	padding-bottom:0px;
	background-color:#AAAAAA;
	margin:auto;
}
div#column1 {
	float:left;
	background-color:#CCFFCC;
	width:200px;
}
div#column2 {
	float:left;
	background-color:#CCCCFF;
	width:300px;
}
div#footer{
	width:500px;
	overflow:auto;
	padding-bottom:0px;
	background-color:#FFCCFF;
	margin:auto;
}
div p{
	padding:10px;
}
  &lt;/style&gt;
 &lt;/head&gt;
 &lt;body&gt;
  &lt;h1&gt;title&lt;/h1&gt;
  &lt;div id=&quot;outer&quot;&gt;
   &lt;div id=&quot;column1&quot;&gt;
    &lt;p&gt;
     Left
     &lt;br&gt;
     Column
     &lt;br&gt;
     Floated
     &lt;br&gt;
     Left
    &lt;/p&gt;
   &lt;/div&gt;
   &lt;div id=&quot;column2&quot;&gt;
    &lt;p&gt;
     Right Column float with no clears below it...
    &lt;br&gt;
    &lt;/p&gt;
   &lt;/div&gt;
  &lt;/div&gt;
  &lt;div id=&quot;footer&quot;&gt;
   &lt;p&gt;
    FOOTER not cleared...
   &lt;/p&gt;
  &lt;/div&gt;
 &lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://robertdot.org/2007/04/24/easy-clearing-of-floats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
