<?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>Justin Tadlock &#187; JavaScript</title>
	<atom:link href="http://justintadlock.com/tags/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://justintadlock.com</link>
	<description>Life, Blogging, and WordPress</description>
	<lastBuildDate>Fri, 11 May 2012 00:27:57 +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>How To Create Tabs Using jQuery</title>
		<link>http://justintadlock.com/archives/2007/11/07/how-to-create-tabs-using-jquery</link>
		<comments>http://justintadlock.com/archives/2007/11/07/how-to-create-tabs-using-jquery#comments</comments>
		<pubDate>Wed, 07 Nov 2007 22:15:54 +0000</pubDate>
		<dc:creator>Justin Tadlock</dc:creator>
				<category><![CDATA[Web Design]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://justintadlock.com/archives/2007/11/07/how-to-create-tabs-using-jquery</guid>
		<description><![CDATA[Tabbed sidebar elements are in. Why bother making users scroll down the page, when they can simply click a tab to quickly access various parts of your site? Another popular thing is jQuery, &#8220;The Write Less, Do More, JavaScript Library.&#8221; I&#8217;ve gotten a lot of emails asking how I created tabs with jQuery in the [...]]]></description>
			<content:encoded><![CDATA[<p>Tabbed sidebar elements are in.  Why bother making users scroll down the page, when they can simply click a tab to quickly access various parts of your site?  Another popular thing is <a href="http://jquery.com" title="jQuery javascript library"> jQuery</a>, &#8220;The Write Less, Do More, JavaScript Library.&#8221;</p>
<p>I&#8217;ve gotten a lot of emails asking how I created tabs with jQuery in the last couple of days &mdash; since I released the <a href="http://justintadlock.com/archives/2007/11/07/how-to-create-tabs-using-jquery" title="Visionary WordPress theme: A theme for magazine and news sites"> Visionary theme</a>.  It&#8217;s actually not all that hard (I&#8217;m not a JavaScript programmer), and I&#8217;m going to run you through a tutorial.</p>
<p>This tutorial is primarily based off WordPress, but can easily be ported anywhere else.</p>
<p>First, you&#8217;ll need four files.  You need &#8220;sidebar.php,&#8221; &#8220;header.php,&#8221; &#8220;style.css,&#8221; and &#8220;tabs.js&#8221; (you&#8217;ll have to create this file).  Then, you need to download a copy of <a href="http://jquery.com" title="jQuery javascript library"> jQuery</a>.  This file will be named &#8220;jquery.js.&#8221;  Add it to your theme directory along with each of these other files.</p>
<h3>Pulling the jQuery into your blog:</h3>
<p>Open &#8220;header.php&#8221; and add these lines between the <code>&lt;head&gt;&lt;/head&gt;</code> tags:</p>
<pre><code>&lt;script type="text/javascript" src="&lt;?php echo bloginfo(stylesheet_directory) .'/jquery.js'; ?&gt;"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="&lt;?php echo bloginfo(stylesheet_directory) .'/tabs.js'; ?&gt;"&gt;&lt;/script&gt;</code></pre>
<p>Now, you can close that file.  You won&#8217;t need it anymore.  All this does is include jQuery and our custom script on every page of your site.  You can now use them anywhere.</p>
<h3>Creating the sidebar:</h3>
<p>First, we need to open &#8220;sidebar.php&#8221; and input this code.  This is the XHTML we&#8217;re using.  I&#8217;m leaving the content inside the tabs open.  Do what you want with it.  You shouldn&#8217;t change the specific classes used because we&#8217;ll need them when using jQuery.  If you change them here, you&#8217;ll have to change them in your &#8220;tabs.js&#8221; file.</p>
<pre><code>
&lt;div class="tabbed"&gt;
	&lt;!-- The tabs --&gt;
	&lt;ul class="tabs"&gt;
	&lt;li class="t1"&gt;&lt;a class="t1 tab" title="&lt;?php _e('Tab 1'); ?&gt;"&gt;&lt;?php _e('Tab 1'); ?&gt;&lt;/a&gt;&lt;/li&gt;
	&lt;li class="t2"&gt;&lt;a class="t2 tab" title="&lt;?php _e('Tab 2'); ?&gt;"&gt;&lt;?php _e('Tab 2'); ?&gt;&lt;/a&gt;&lt;/li&gt;
	&lt;li class="t3"&gt;&lt;a class="t3 tab" title="&lt;?php _e('Tab 3'); ?&gt;"&gt;&lt;?php _e('Tab 3'); ?&gt;&lt;/a&gt;&lt;/li&gt;
	&lt;li class="t4"&gt;&lt;a class="t4 tab" title="&lt;?php _e('Tab 4'); ?&gt;"&gt;&lt;?php _e('Tab 4'); ?&gt;&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;

	&lt;!-- tab 1 --&gt;
	&lt;div class="t1"&gt;
	&lt;!-- Put what you want in here.  For the sake of this tutorial, we'll make a list.  --&gt;
	&lt;ul&gt;
		&lt;li&gt;List item&lt;/li&gt;
		&lt;li&gt;List item&lt;/li&gt;
		&lt;li&gt;List item&lt;/li&gt;
		&lt;li&gt;List item&lt;/li&gt;
		&lt;li&gt;List item&lt;/li&gt;
	&lt;/ul&gt;
	&lt;/div&gt;

	&lt;!-- tab 2 --&gt;
	&lt;div class="t2"&gt;
	&lt;!-- Or, we could put a paragraph --&gt;
		&lt;p&gt;This is a paragraph about the jQuery tabs tutorial.&lt;/p&gt;
	&lt;/div&gt;

	&lt;!-- tab 3 --&gt;
	&lt;div class="t3"&gt;
	&lt;!-- Or, we could add a div --&gt;
		&lt;div&gt;Something needs to go in here!&lt;/div&gt;
	&lt;/div&gt;

	&lt;!-- tab 4 --&gt;
	&lt;div class="t4"&gt;
	&lt;!-- Why not put a few images in here? --&gt;
		&lt;p&gt;
			&lt;img src="image.gif" alt="Sample" /&gt;
			&lt;img src="image.gif" alt="Sample" /&gt;
			&lt;img src="image.gif" alt="Sample" /&gt;
		&lt;/p&gt;
	&lt;/div&gt;

&lt;/div&gt;&lt;!-- tabbed --&gt;</code></pre>
<p>Now, our sidebar is set up.  You can close &#8220;sidebar.php.&#8221;</p>
<h3>The backbone, jQuery:</h3>
<p>Open, or create, the file &#8220;tabs.js&#8221; for this part of the tutorial.  This is the heart of our script.  I&#8217;m sure there&#8217;s a better way to do this, but since so many people have requested this, they&#8217;ll have to stick with my version I guess.  Copy and paste this code into your file.</p>
<p><strong>Updated:</strong> Thanks to Karl from <a href="http://learningjquery.com" title="Learning jQuery"> Learning jQuery</a> for providing better code for this.</p>
<pre><code>$(document).ready(function() {
// setting the tabs in the sidebar hide and show, setting the current tab
	$('div.tabbed div').hide();
	$('div.t1').show();
	$('div.tabbed ul.tabs li.t1 a').addClass('tab-current');

// SIDEBAR TABS
$('div.tabbed ul li a').click(function(){
	var thisClass = this.className.slice(0,2);
	$('div.tabbed div').hide();
	$('div.' + thisClass).show();
	$('div.tabbed ul.tabs li a').removeClass('tab-current');
	$(this).addClass('tab-current');
	});
});</code></pre>
<p>You can close this file once you&#8217;re finished pasting the above code in.</p>
<h3>Styling the sidebar:</h3>
<p>Since I can&#8217;t decide how to style your sidebar for you, I&#8217;ll just leave you with things styled enough to get your started.  Of course, you can do anything you want with styling.  The tabbed sidebar has a lot of different classes that you can make use of.  Or, you can even add your own.  Use this code in your &#8220;style.css&#8221; file.  Make adjustments to suit your needs.</p>
<pre><code>/* Contains the entire tabbed section */
.tabbed {
	}

/* List of tabs */.tabbed ul.tabs {
	float: left;
	display: inline;
	width: 100%;
	margin: 0;
	padding: 0;
	}
.tabbed ul.tabs li {
	list-style: none;
	float: left;
	margin: 0;
	padding: 0;
	}
.tabbed ul.tabs li a {
	overflow: hidden;
	display: block;
	margin: 0 2px 0 0;
	padding: 10px 12px;
	}
.tabbed ul.tabs li a:hover {
	}

/* The current selected tab */
.tabbed ul.tabs li a.tab-current {
	}

/* The content shown when a tab is selected */
.tabbed div {
	float: left;
	display: block;
	width: 100%;
	}

/* Set the CSS to make sure the other tabs' content isn't shown other than the first */
.tabbed div.t2, .tabbed div.t3, .tabbed div.t4 {
	display: none;
	}

/* Content for inside your tabs' divs */
.tabbed div ul {
	}
.tabbed div p {
	}
.tabbed div div {
	}</code></pre>
<p>There you have it.  It seems like a good bit of code, but it really isn&#8217;t too bad.  Most of the stuff you won&#8217;t have to touch too often.  Just get everything set up and style it.</p>
<p>I hope this helps, and if you find any bugs, make sure to report them.</p>
]]></content:encoded>
			<wfw:commentRss>http://justintadlock.com/archives/2007/11/07/how-to-create-tabs-using-jquery/feed</wfw:commentRss>
		<slash:comments>106</slash:comments>
		</item>
	</channel>
</rss>

