I am writing some code for a website that uses 4 list items to show/hide 4 different divs at a different place on the page.
Here is a rough example of what I am doing:
<ul>
<li id="first">One</li>
<li id="second">Two</li>
<li id="third">Three</li>
<li id="fourth">Four</li>
</ul>
<div id="one">This is the first div</div>
<div id="two">This is the second div</div>
<div id="three">This is the third div</div>
<div id="four">This is the four div</div>
And I’ve got some ugly arse JS code going on, and for the life of me I can’t remember/figure out how to simplify it at all.
$("#first").click(function() {
$("#one").fadeIn();
$("#two, #three, #four").fadeOut();
});
$("#second").click(function() {
$("#two").fadeIn();
$("#one, #three, #four").fadeOut();
});
$("#third").click(function() {
$("#three").fadeIn();
$("#two, #one, #four").fadeOut();
});
$("#fourth").click(function() {
$("#four").fadeIn();
$("#two, #three, #one").fadeOut();
});
This does what I need it to, but I know there has to be a simpler way of doing it. Here is a JSFiddle of it working – http://jsfiddle.net/claycauley/FBrx5/
If any one can help me understand why/how to simplify it that would be great also, because I am trying to learn but getting stumped.
Try:
http://jsfiddle.net/FBrx5/1/