Reputation: 3549
I have a requirement that when I clicked on any of tab, it is working like accordion menu tabs, but here one problem is when I clicked on any other tab, the opened tab should be closed only current tab related content should display, as html I mentioned is sample only, actually the id's and div's are dynamically generating. I am also inserting the picture in order to understand better.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"/>
<script>
$(document).ready(function(){
// Get all the links.
var link = $("#vy_accordion a");
// On clicking of the links do something.
link.on('click', function(e) {
e.preventDefault();
var a = $(this).attr("href");
$(a).slideToggle('fast');
$(".control-group").css({ 'display' : 'block', 'margin-bottom' : '0' });
});
});
</script>
#vy_accordion {
margin-top: 10px;
border: thin solid #cecece;
border-top: none;
border-bottom: none;
}
#vy_accordion div {
background: white;
/*height: inherit;
line-height: inherit;*/
display: none;
border-bottom: thin solid #cecece;
padding-left: 15px;
min-height: 70px;
}
a.divlink {
display: block;
/* width: 483px; */
background: #f4f4f4;
background-image: -webkit-linear-gradient(white, #ededed);
background-image: -moz-linear-gradient(white, #ededed);
background-image: -o-linear-gradient(white, #ededed);
background-image: -ms-linear-gradient(white, #ededed);
background-image: linear-gradient(white, #ededed);
color: #959696;
padding-left: 15px;
height: 40px;
line-height: 40px;
text-decoration: none;
border-bottom: thin solid #cecece;
border-top: thin solid #cecece;
font-family: Arial;
font-size: 13px;
font-weight: bold;
text-shadow: 0px 1px 1px white;
}
<a class="divlink" href="#Menu-hover-color">Menu-hover-color</a>
<div id="Menu-hover-color" style="display: none;">
<div class="control-group">
<label class="control-label" for="_156_Menu-hover-color"> Menu-hover-color </label> <input class="field" id="_156_Menu-hover-color" name="" type="text" value="#B3E5FC">
</div>
</div>
<a class="divlink" href="#Menu-hover-color">Menu-item-color</a>
<div id="Menu-item-color" style="display: none;">
<div class="control-group">
<label class="control-label" for="_156_Menu-item-color"> Menu-hover-color </label> <input class="field" id="_156_Menu-item-color" name="" type="text" value="#B3E5FC">
</div>
</div>
Upvotes: 1
Views: 280
Reputation: 4730
Add similar class to your tabs (e.g. linkTab
) and based on this class-selector call hide()
before showing the clicked / selected tab, as following:
HTML:
<a class="divlink" href="#Menu-hover-color">Menu-hover-color</a>
<div class="linkTab" id="Menu-hover-color" style="display: none;">
...
JS:
var link = $("#vy_accordion a");
// On clicking of the links do something.
link.on('click', function(e) {
e.preventDefault();
var a = $(this).attr("href");
// this line will hide all open tab based on class selector
$('.linkTab').hide();
$(a).slideToggle('fast');
$(".control-group").css({ 'display' : 'block', 'margin-bottom' : '0' });
});
Upvotes: 1