Using JavaScript to dynamically swap show/hide and add active classes to anchor links?
<p>Here is my scenario. I'm am HTML/CSS guy, JavaScript not so much. But this is a JavaScript problem.</p>

<p>I'm building out a new resume site for myself; <a href="http://banderdash.net/design/" rel="nofollow">http://banderdash.net/design/</a> pardon the typography, I have yet to bring in Typekit and really get the look 'singing'. </p>

<p>In a perfect world, I would have a script that could perform the following:</p>

<p>-Onclick the .mainnav (along the top), would swap the class="active" for the link clicked. It would also switch the class="active" in the .subnav, and swap which content block is being shown in the black area. (about, work, ramblings, or contact)</p>

<p>-Onclick the .subnav would do exactly the same thing (switch active class on itself and mainnav and swap the content block).</p>

<p>-Onload all of the content blocks, except ABOUT, would be hidden. I want this so users without JavaScript aren't missing out on the content. Currently I have a class of hide on all of them except About. This is no good for accessibility.</p>

<p>What my script is is doing now:</p>

<pre><code>$(document).ready(function(){
$(".subnav a, .mainnav a").click(function(){
//remove possible hilights
$(".subnav a, .mainnav a").removeClass("active");
//hilight the clicked link
$(this).addClass("active");

//hide possible shown content
$(".content").hide();
//show my content
var myid = $(this).attr("id");
$("#" + myid + "-content").show();
});
});
</code></pre>

<p>Here is my mainnav:</p>

<pre><code>&lt;ul class="mainnav"&gt;
&lt;li class="first"&gt;&lt;a class="active" href="/design"&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a id="nav-about" href="#about"&gt;About&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a id="nav-work" href="#work"&gt;Work&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a id="nav-ramblings" href="#ramblings"&gt;Ramblings&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a id="nav-contact" href="#contact"&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<p>Here is my subnav:</p>

<pre><code>&lt;ul class="subnav"&gt;
&lt;li&gt;&lt;a id="nav-about" class="active" href="#about"&gt;About&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a id="nav-work" href="#work"&gt;Work&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a id="nav-ramblings" href="#ramblings"&gt;Ramblings&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a id="nav-contact" href="#contact"&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<p>Simplified markup of the content blocks:</p>

<pre><code>&lt;div id="nav-about-content" class="content about"&gt;
&lt;p&gt;Yada&lt;/p&gt;
&lt;/div&gt;

&lt;div id="nav-work-content" class="content work hide"&gt;
&lt;p&gt;Yada&lt;/p&gt;
&lt;/div&gt;

&lt;div id="nav-ramblings-content" class="content ramblings hide"&gt;
&lt;p&gt;Yada&lt;/p&gt;
&lt;/div&gt;

&lt;div id="nav-contact-content" class="content contact hide"&gt;
&lt;p&gt;Yada&lt;/p&gt;
&lt;/div&gt;
</code></pre>

<p>As I mentioned I would like to get rid of the hide class I have hard coded in on the content blocks other then about. I would like them to be added back by an onload function. This way they are hidden when JavaScript is enabled, and show when it's not.</p>

<p>Right now everything is working except the active class being swapped for both the subnav link and the mainnav link, when either is clicked. Not sure how to deal with this. Also it doesn't degrade with no JavaScript most of the content is in-accessible. And I have to use the id="nav-about" for both the .mainnav and the .subnav - which is not valid XHTML.</p>

<p>Would hug you (virtually) if you could get me going again. I'm stuck.</p>
