Reputation: 1103
HTML
<p class="heading"></p>
<div id="div1" class="targetDiv">content1</div>
<p class="heading"></p>
<div id="div2" class="targetDiv">content2</div>
<p class="heading"></p>
<div id="div3" class="targetDiv">content3</div>
<p class="heading"></p>
<div id="div4" class="targetDiv">content4</div>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(".targetDiv").hide();
//toggle the componenet
jQuery(".heading").click(function()
{
jQuery(this).next(".targetDiv").slideToggle(500);
});
});
</script>
here i'm able to show one div on clicking heading but when i'm going to open another it must get hidden. Infact while i'm opening one div other must get hidden.
pls help me...
Thanks in advance
Upvotes: 0
Views: 381
Reputation: 2943
Do you mean something like this:
Just add the jQuery(".targetDiv").hide(); into the click function too.
Upvotes: 0
Reputation: 123377
$(".heading").click(function() {
$('.targetDiv:visible').hide(); /* reference to the single previous
* opened tab and hide it
*/
$(this).next(".targetDiv").slideToggle(500);
});
Upvotes: 1
Reputation: 10340
This should do the trick:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(".targetDiv").hide();
//toggle the componenet
jQuery(".heading").click(function()
{
jQuery(".targetDiv").slideUp();
jQuery(this).next(".targetDiv").slideToggle(500);
});
});
</script>
A small explanation: It first hides all divs and then opens one. I should do so fast that you won't be able to see it.
Upvotes: 1