HTML:
<div id="bottomContent" class="bottom_content" > <p>First Content</p> </div>
<div id="bottomContent2" class="bottom_content" > <p>second Content</p> </div>
<div id="bottomContent3" class="bottom_content" > <p>Third Content</p> </div>
JS:
$("div.one").click (function(){
$("#bottomContent2").hide();
$("#bottomContent3").hide();
$("#bottomContent").animate({width: "toggle"}, 1000);
});
$("div.two").click (function(){
$("#bottomContent").hide();
$("#bottomContent1").hide();
$("#bottomContent2").animate({width: "toggle"}, 1000);
});
$("div.three").click (function(){
$("#bottomContent").hide();
$("#bottomContent2").hide();
$("#bottomContent3").animate({width: "toggle"}, 1000);
});
I have the following jQuery code whereby when each of the divs is clicked, it will animate its own corresponding div item and close any other div that may be already opened but I am sure there are better ways to code it and make it more compact than I have done. Any help will be appreciated.
I always approach this problem by putting some extra metadata (using
data-*attributes) on the element being clicked to associated it’s content:Notice ive also given all three div’s the same class, enabling me to associate the same action with all three.
Then the jQuery becomes:
Live example: http://jsfiddle.net/rHKML/