<?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>Povert &#187; CSS</title>
	<atom:link href="http://www.povert.com/category/technology/world-wide-web-related-code-discussion/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.povert.com</link>
	<description>It&#039;s Pronounced &#34;Pah-vert.&#34;  You povert.</description>
	<lastBuildDate>Fri, 27 Aug 2010 16:18:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>CSS-centric JS</title>
		<link>http://www.povert.com/2008/09/11/css-centric-js/</link>
		<comments>http://www.povert.com/2008/09/11/css-centric-js/#comments</comments>
		<pubDate>Thu, 11 Sep 2008 20:29:26 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[WWW]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.povert.com/?p=793</guid>
		<description><![CDATA[This probably isn&#8217;t news to anyone deep in JS/CSS land, but I thought these notes may be handy for someone who&#8217;s still figuring out the best way to approach some problems. There&#8217;s a temptation, I think, to go overboard with JS when manipulating elements on a page. Many tasks can be more simply handled with [...]]]></description>
			<content:encoded><![CDATA[<p>This probably isn&#8217;t news to anyone deep in JS/CSS land, but I thought these notes may be handy for someone who&#8217;s still figuring out the best way to approach some problems.</p>
<p>There&#8217;s a temptation, I think, to go overboard with JS when manipulating elements on a page.  Many tasks can be more simply handled with a combination of JS and CSS.</p>
<p>For example, let&#8217;s say you have a simple unordered list:<br />
<code><br />
&lt;ul id="happyList"&gt;<br />
&nbsp;&nbsp;&lt;li&gt;&lt;a href="tacos.html"&gt;Tacos&lt;/a&gt;&lt;/li&gt;<br />
&nbsp;&nbsp;&lt;li&gt;&lt;a href="burritos.html"&gt;Burritos&lt;/a&gt;&lt;/li&gt;<br />
&nbsp;&nbsp;&lt;li&gt;&lt;a href="enchiladas.html"&gt;Enchiladas&lt;/a&gt;&lt;/li&gt;<br />
&nbsp;&nbsp;&lt;li&gt;&lt;a href="tamales.html"&gt;Tamales&lt;/a&gt;&lt;/li&gt;<br />
&nbsp;&nbsp;&lt;li&gt;&lt;a href="chalupas.html"&gt;Chalupas&lt;/a&gt;&lt;/li&gt;<br />
&nbsp;&nbsp;&lt;li&gt;&lt;a href="antacids.html"&gt;Antacids&lt;/a&gt;&lt;/li&gt;<br />
&lt;/ul&gt;<br />
</code></p>
<p>Let&#8217;s say that you only want 3 items to show by default, and you want a &#8220;More&#8221; link at the bottom that will expand the list to show the rest.</p>
<p>There&#8217;s a few ways you could approach this.  People may be tempted to put an onclick event onto the &#8220;More&#8221; link, then have that call a function that toggles the visibility of each element after the third.  Something like:</p>
<p><code><br />
var happy = document.getElementById('happyList');<br />
var items = happy.getElementsByTagName('li');<br />
function showLinks() {<br />
&nbsp;&nbsp;for (var i=3; i &lt; items; i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;items[i].style.display = 'list-item';<br />
&nbsp;&nbsp;}<br />
}<br />
function hideLinks() {<br />
&nbsp;&nbsp;for (var i=3; i &lt; items; i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;items[i].style.display = 'none';<br />
&nbsp;&nbsp;}<br />
}<br />
</code></p>
<p>That would work (though I haven&#8217;t actually tested this particular js), but it&#8217;s definitely the crappy way, for a number of reasons.  First, there&#8217;s no need for two functions, though that&#8217;s beside the point of this post.  Second, we&#8217;re running through a for() loop every time each function is called.</p>
<p>Instead of something like that, the way I&#8217;d do it is to first set up this CSS:</p>
<p><code><br />
#happyList.collapsed li {<br />
&nbsp;&nbsp;display: none;<br />
}<br />
#happyList.collapsed li.alwaysShow {<br />
&nbsp;&nbsp;display: list-item;<br />
}<br />
</code></p>
<p>Then, you put a class of &#8216;alwaysShow&#8217; on the first three list item elements, either in the html itself or with javascript:</p>
<p><code><br />
function assignShow() {<br />
&nbsp;&nbsp;var happy = document.getElementById('happyList');<br />
&nbsp;&nbsp;var items = happy.getElementsByTagName('li');<br />
&nbsp;&nbsp;for (var i=0; i &lt; items.length &#038;&#038; i &lt; 3; i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;items[i].className = 'alwaysShow';<br />
&nbsp;&nbsp;}<br />
}<br />
</code></p>
<p>Now, when the ul has a class of &#8216;collapsed&#8217;, only the first three items show:</p>
<p><code><br />
function toggleList() {<br />
&nbsp;&nbsp;var happy = document.getElementById('happyList');<br />
&nbsp;&nbsp;if (happy.className == 'collapsed') {<br />
&nbsp;&nbsp;&nbsp;&nbsp;happy.className = '';<br />
&nbsp;&nbsp;} else {<br />
&nbsp;&nbsp;&nbsp;&nbsp;happy.className = 'collapsed';<br />
&nbsp;&nbsp;}<br />
}<br />
</code></p>
<p>Or, more succinctly:<br />
<code><br />
function toggleList() {<br />
&nbsp;&nbsp;var happy = document.getElementById('happyList');<br />
&nbsp;&nbsp;happy.className = happy.className ==<br />
&nbsp;&nbsp;&nbsp;&nbsp;'collapsed' ? '' : 'collapsed';<br />
}<br />
</code></p>
<p>Then, either in HTML or using javascript, you can add an element with the &#8220;More&#8221; text and attach an event to it that calls toggleList().</p>
<p>I&#8217;m being a little vague on details here.  My main point is that it&#8217;s preferable to lean on CSS as much as possible.  It helps to avoid micromanaging elements.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.povert.com/2008/09/11/css-centric-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
