<?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>Sazbean&#187; Quick Bites</title>
	<atom:link href="http://sazbean.com/category/code/quick-bites/feed/" rel="self" type="application/rss+xml" />
	<link>http://sazbean.com</link>
	<description>Internet Marketing Strategy</description>
	<lastBuildDate>Tue, 07 Feb 2012 15:00:16 +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>Quick bite &#8211; show / hide button in Rails, RJS</title>
		<link>http://sazbean.com/2008/08/27/quick-bite-show-hide-button-in-rails-rjs/</link>
		<comments>http://sazbean.com/2008/08/27/quick-bite-show-hide-button-in-rails-rjs/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 14:52:38 +0000</pubDate>
		<dc:creator>Aaron Worsham</dc:creator>
				<category><![CDATA[Quick Bites]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rjs]]></category>

		<guid isPermaLink="false">http://sazbean.com/?p=216</guid>
		<description><![CDATA[Sazbean is about online business strategy and we work hard to keep this blog as accessible to our readers as possible. &#8216;Say it with words, not code&#8217; is one of our guiding principles which keeps us honest when covering technical topics with applied business uses. A principle, I should add, that I will be flagrantly&#8230;<br /><span class="more-link-wrapper"><a href="http://sazbean.com/2008/08/27/quick-bite-show-hide-button-in-rails-rjs/" class="more-link">Read More</a></span>]]></description>
			<content:encoded><![CDATA[<p>Sazbean is about online business strategy and we work hard to keep this blog as accessible to our readers as possible.  &#8216;Say it with words, not code&#8217; is one of our guiding principles which keeps us honest when covering technical topics with applied business uses.  A principle, I should add, that I will be flagrantly ignoring for this post.</p>
<p>It isn&#8217;t always wise to hold back that urge to express our deeper technical understand of a subject we are passionate about.   Instead, we have opted to create a segment called quick bites that will be short, technically focused example posts about some subject we are currently working.</p>
<p>I was recently building a UI for a client in Rails and found a good use for a show/hide button in the design.  Show/hide buttons, as their names describe, reveal content when pressed and then hide it when pressed again.  Typical example code for this using Rails and Javascript might be&#8230;</p>
<p><span id="more-216"></span>[sourcecode language='rails']</p>
<div>
<%=link_to_function ("show", "page['content').show()")%><br />
<%=link_to_function ("hide", "page['content').hide()")%></p>
<div id="content">
This is the content
</div>
</div>
<p>[/sourcecode]</p>
<p>Each link_to_function is a call in Rails to have the renderer build JavaScript that points to a local JS function in the browser that shows or hides the &#8216;content&#8217; element.  This, therefore, it not AJAX but merely local Javascript.  The problem with this example is the use of two links instead of one, which is bad UI form.  One way to combine the action would be to use Prototype&#8217;s toggle() function, as such</p>
<p>[sourcecode language='rails']</p>
<div>
<%=link_to_function ("toggle", "page['content').toggle()")%></p>
<div id="content">
This is the content
</div>
</div>
<p>[/sourcecode]</p>
<p>Now the action is combined but there is a problem.  Since the href text does not change, you need to use a word that is abmigious enough to work in both a show state and a hide state, like &#8216;toggle&#8217;.  Yet this is not intuitive enough to the reader.  Toggle what?  While I played around with having the function rewrite the text in the href tag from &#8216;show&#8217; to &#8216;hide&#8217;, I found this cumbersome.  A better way is to have what appears to be one button serving different functions.  The following code shows a few improvements, actually</p>
<p>[sourcecode language='rails']</p>
<div id=<%= "'entity#{entity.id.to_s}'" %>><br />
<span id=<%="'more_info_show#{entity.id.to_s}'"%>><br />
<span id="show_hide"><br />
<%= link_to_function ("Show", nil) do |page|<br />
page["content#{entity.id.to_s}"].toggle()<br />
page["more_info_hide#{entity.id.to_s}"].toggle()<br />
page["more_info_show#{entity.id.to_s}"].toggle()<br />
end %><br />
</span><br />
</span><br />
<span id=<%="'more_info_hide#{entity.id.to_s}'"%> style=&#8221;display:none;&#8221;><br />
<span id="show_hide"><br />
<%= link_to_function ("Hide", nil) do |page|<br />
page["content#{entity.id.to_s}"].toggle()<br />
page["more_info_hide#{entity.id.to_s}"].toggle()<br />
page["more_info_show#{entity.id.to_s}"].toggle()<br />
end%><br />
</span><br />
</span></p>
<div id= <%= "'content#{entity.id.to_s}'" %> style = &#8220;display:none;&#8221; ></p>
<div style="width:100%">
<%= entity.blurb %>
</div>
</div>
</div>
<p>[/sourcecode]</p>
<p>First, you&#8217;ll see the id=&lt;%= &#8220;&#8216;entity#{entity.id.to_s}&#8217;&#8221; which is a way to have custom element names in an index of items.  The double + single quotes are important to the renderer.   Next you see the Show link with a block of three toggle commands.  Blocks are a way in RJS to tell the browser to do actions in an ordered chain.  Here we are toggling the content to show, toggling the hide link to show and toggling the show link to hide.  Once hidden, the show button isn&#8217;t available and its place in the browser is taken by the hide button, which has the same toggle functions to reverse the settings.  Important to note that in this example, the content starts like hidden by the inline  style=&#8221;display:none;&#8221; CSS.  It needs to be inline to work.</p>
<p>I hope this little diversion into the deeper waters of user interface wasn&#8217;t too far afield for the readers.   Please let me know your thoughts.</p>
<p><strong><span style="color:#3366ff;">If you liked this article, consider subscribing to this blog via <a href="http://www.feedburner.com/fb/a/emailverifySubmit?feedId=1163671&amp;loc=en_US">email</a> or <a href="http://feeds.feedburner.com/Sazbean">RSS</a>.  Also, consider <a href="http://sazbeanconsulting.com/subscribe">subscribing to have our free weekly newsletter</a> sent to your email inbox.</span></strong></p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: left; margin-left: 20px; margin-bottom: 5px; margin-right: 10px;"><g:plusone size="medium" count="1" href="http://sazbean.com/2008/08/27/quick-bite-show-hide-button-in-rails-rjs/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://sazbean.com/2008/08/27/quick-bite-show-hide-button-in-rails-rjs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

