I am trying to build a feature into my site where I have a toggle button. When the button is depressed a div should show and when the button is normal, the div should be invisible. There is also a close 'X' on the div itself, which, if clicked, should make the button normal (ie not depressed)
The problem is that I don't know how to un-activate the button. I can remove the class active
, which updates the DOM, but it comes back from somewhere within bootstrap (Chrome breakpoint on DOM change showed me a stack trace)
Here is my code:
var addInputButton = function(id, txt, clk) {
var btn = $('<a>')
.text(txt)
.attr("id", id)
.addClass("btn")
.addClass("btn-mini")
.attr("data-toggle", "button")
//.addClass("btn-info")
.click(clk);
$('#input_help_section')
.append(btn);
return btn;
};
// CHEATSHEET -----------------
(function() {
var toggleName = "markdownCheatSheetIsVisible";
var btnId = "markdownCheatsheetToggleButton";
var box = $("#markdownCheatsheet");
var showBox = function() {
var cheatSheetButton = $('#' + btnId);
if (!cheatSheetButton.hasClass("active")) {
cheatSheetButton.toggle();
cheatSheetButton.show();
}
box.show();
};
var hideBox = function() {
var cheatSheetButton = $('#' + btnId);
if (cheatSheetButton.hasClass("active")) {
cheatSheetButton.toggle();
cheatSheetButton.show();
}
box.hide();
};
addInputButton(btnId, "Markdown cheatsheet", function(event) {
if ($('#' + btnId).hasClass("active") === false) {
showBox();
} else {
hideBox();
}
});
$("#markdownCheatsheetClose").click(function() {
hideBox();
});
// start shown
showBox();
})();
There are a bunch of odd things here:
- I tried to do
addClass('active')
andremoveClass('active')
in theshowBox
andhideBox
functions, but a trigger somewhere in bootstrap kept on adding theactive
class back after I removed it. - The
toggle
method is defined in bootstrap and as far as I can figure it out is meant to toggle theactive
class, but it also hides it (somewhere) and I can't toggle it away from an active state, which I don't quite understand.