/    Sign up×
Community /Pin to ProfileBookmark

Simple class switcher

Disclaimer: I suck at JavaScript. (A lot)

Ok, so I have this:

[code=html] <ul class=”infoTabs”>
<!–
JS:

show(boxid){
//hide (all 3)
//show the selected one
}

–>
<li class=”show” onClick=”show(‘firstinfo’)”>Plat</li>
<li class=”hide” onClick=”show(‘secondinfo’)”>Lots</li>
<li class=”hide” onClick=”show(‘thirdinfo’)”>Misc</li>
</ul>
<div id=”infoTabBox”>
<section class=”show” id=”firstinfo”>
<h3>First Information</h3>
</section>
<section class=”hide” id=”secondinfo”>
<h3>Second Information</h3>
</section>
<section class=”hide” id=”thirdinfo”>
<h3>Third Information</h3>
</section>
</div>[/code]

I’d like to click the list items and have them change the class from “show” to “hide” for all 3 of the section-boxes and then take the one that was selected (the ID is the variable that gets passed by those buttons) and change that one to “show”…

Any suggestions?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@hyperionXSMar 02.2012 — IN order to work faster, use jQuery.
[code=html]
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" ></script>
<style>
.show{color:green}
.hide{color:red}
</style>
<script type="text/javascript">
function show(id){
$('.section').removeClass('show').addClass('hide');
$('#'+id).addClass('show').removeClass('hide');
}
</script>
<ul class="infoTabs">
<li onClick="show('firstinfo')">Plat</li>
<li onClick="show('secondinfo')">Lots</li>
<li onClick="show('thirdinfo')">Misc</li>
</ul>
<div id="infoTabBox">
<section class="section show" id="firstinfo">
<h3>First Information</h3>
</section>
<section class="section hide" id="secondinfo">
<h3>Second Information</h3>
</section>
<section class="section hide" id="thirdinfo">
<h3>Third Information</h3>
</section>
</div>
[/code]
×

Success!

Help @amandaNHT spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...