I have four buttons which has click able property. Clicking on button will make a div slide down and clicking again on same div should close the div. I want to add a condition like, when I have a div open, the click property on rest of the three buttons should be disabled, what I did is
for (var i = 1; i <= 4; i++) {
$(".slide" + i).click(function () {
var openTab = $(this).attr('class');
openTab = openTab.replace('slide', '');
var facetGroup = $(this).attr("key");
if ($('#panel').is(':visible')) {
buttonCloser(openTab);
} else {
buttonOpener(openTab, facetGroup);
}
});
}
function buttonCloser(m) {
for (var j = 1; j <= 4; j++) {
if (j != m) {
//alert(j);
$(".slide" + j).bind("click");
} else {
$(".slide" + j).css({
"background-color": " #fff5c3",
"color": "#000000"
});
}
}
$("#panel").slideUp("slow");
}
function buttonOpener(m, n) {
for (j = 1; j <= 4; j++) {
if (j != m) {
$(".slide" + j).unbind("click");
} else {
$(".slide" + j).css({
"background-color": "#293345",
"color": "#fff5c3"
});
}
}
$("#panel").slideDown("slow");
refreshFacet(n);
}
The problem with this code is that the first time I open a div by clicking on slider, the other three click events are disabled, bt when I close that div, it will nt re-enable its click property. so it wont open anything..
Without seeing your actual mark-up, I’d suggest that you use simple jQuery, rather than mixing and matching between ‘plain’ JavaScript and jQuery:
JS Fiddle demo.
References:
addClass().attribute^="value"selector.hasClass().removeClass().slideToggle().