I have two div's that I open that kinda belong together, but for programming reasons can't be connected. I simplified the code here because it's a lot of code. This is the basic.
<div id="container">
<div id="div1">
<div> when I am clicked I will open div2 </div>
</div>
<div id="div2">
<div> I can be clicked and all the div's stay open </div>
</div>
</div>
And this is the connected JavaScript
$(document).mouseup(function (e) {
var container = $("#div1");
if (!container.is(e.target) && container.has(e.target).length === 0) {
container.hide();
}
});
$(document).mouseup(function (e) {
var container = $("#div2");
if (!container.is(e.target) && container.has(e.target).length === 0) {
container.hide();
}
});
Now what I would like to do is when I click outside div1 it should close div1.
When I click on div1 it opens div2.
Currently with the above when I click in div2 it closes div1, because it is not a child of div1. So it should keep div2 opened.
And when I click outside div1 or div2 it should close the two div's.
How can I combine the two JavaScript codes to get the explained behaviour?
Summary of what I try to accomplish:
- Click on the div inside
div1will opendiv2 - Click outside
div1ordiv2will close bothdiv1anddiv2. - In case the div inside
div1is not clicked and one clicks outsidediv1, it should closediv1.