I have a series of dropdown buttons, each with its own separate content holder. I want each to toggle display when clicked, but also hide display when clicking anywhere else on the page (including on one of the other buttons) other than inside the content holder.
Here is the code:
$(document).on({
click: function (event) {
$("#content1").toggle();
$(document).on({
click: function HideMenu1(event) {
if ($(event.target).attr("id") != "content1") {
$("#content1").hide();
$(document).off("click", HideMenu1);
}
}
});
}
}, "#menu1");
$(document).on({
click: function (event) {
$("#content2").toggle();
$(document).on({
click: function HideMenu2() {
if ($(event.target).attr("id") != "content2") {
$("#content2").hide();
$(document).off("click", HideMenu2);
}
}
});
}
}, "#menu2");
$(document).on({
click: function (event) {
$("#content3").toggle();
$(document).on({
click: function HideMenu3() {
if ($(event.target).attr("id") != "content3") {
$("#content3").hide();
$(document).off("click", HideMenu3);
}
}
});
}
}, "#menu3");
<div id=menu1>Menu #1</div>
<div id=content1 style='display:none'>Contents of menu #1</div>
<div id=menu2>Menu #2</div>
<div id=content2 style='display:none'>Contents of menu #2</div>
<div id=menu3>Menu #3</div>
<div id=content3 style='display:none'>Contents of menu #3</div>
It is working... EXCEPT if you click one of the elements, click again to close it, and then try to click it a third time. Then it won't open until you click somewhere else first.
Example -- > http://jsfiddle.net/LsNZx/
I know I'm close... help!