I'm trying to create a page that will basically have 3 sections within it. At any given point, only 1 section should be displayed. Although relatively new to javascript/jquery, I figured this would be pretty straightforward with some links & a switch statement, but am running into a wall & can't figure out where I'm going wrong.
I've set up a fiddle at http://jsfiddle.net/donschaeferart/LVXKb/2/ that re-creates the basic functionality I'm trying to achieve. The code is as follows:
HTML:
<div class="divMain">
<div id="divSub1">
<h1>SubSection 1</h1>
<p><a href="" onclick="selectSection(2);">Next</a></p>
</div>
<div id="divSub2" class="displayNone">
<h1>SubSection 2</h1>
<p><a href="" onclick="selectSection(1);">Previous</a></p>
<p><a href="" onclick="selectSection(3);">Next</a></p>
</div>
<div id="divSub3" class="displayNone">
<h1>SubSection 3</h1>
<p><a href="" onclick="selectSection(2);">Previous</a></p>
</div>
</div>
CSS:
.displayNone{display:none;}
Javascript:
function selectSection(n){
switch (n){
case 1:
$("#divSub1").removeClass('displayNone');
$("#divSub2").addClass('displayNone');
$("#divSub3").addClass('displayNone');
break;
case 2:
$("#divSub1").addClass('displayNone');
$("#divSub2").removeClass('displayNone');
$("#divSub3").addClass('displayNone');
break;
case 3:
$("#divSub1").addClass('displayNone');
$("#divSub2").addClass('displayNone');
$("#divSub3").removeClass('displayNone');
break;
default:
break;
}
}
However, while the code is valid & seems like it'd work fine, 2 things are happening...
- When I run similar code on an actual page & click one of the links, it executes, then immediately reverts back after the function completes so that it looks as if the function never executed at all
- When I run the fiddle & try to click a link, the result viewport switches to what appears to be a messed up version of the JSFiddle site itself (if you scroll right & click "login", it takes you to the JSFiddle login screen)
I'm pretty sure there's a minor mistake somewhere that's throwing everything off, but I'm at a loss, so any help would be appreciated.