Whenever I click on the selection criteria box, a dropdown menu appears. Clicking on the white space or the data table button makes the drop-down menu disappear, which is good. However, when I perform the same action for 'choose data table,' the drop-down menu doesn't vanish unless I click on the button again. What could be causing this issue? I suspect it might be due to having identical code in both cases... If I remove the JavaScript code for the right-side window that allows the window to disappear upon clicking on the white space, the opposite occurs (the left box's menu disappears but not the right side). Full code provided below:
/* When the user clicks on the button, toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDatalist").classList.toggle("show")
}
window.onclick = function(event) {
if (!event.target.matches('.dropdata')) {
var dropdowns = document.getElementsByClassName("datalist-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
function criFunction() {
document.getElementById("myCriteria").classList.toggle("show")
}
window.onclick = function(event) {
if (!event.target.matches('.dropcriteria')) {
var dropdowns = document.getElementsByClassName("criterialist-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
/*START Title*/
h1 {
color: #46b3d1;
font-family: Gotham;
font-weight: 80;
}
/*END title*/
/*START Database drop down*/
.dropdata {
font-family: Gotham;
color: white;
padding: 16px;
font-size: 16px;
width:200px;
border: none;
cursor: pointer;
background-color: #46b3d1;
}
.dropdata:hover, .dropdata:focus {
background-color: #22819b
}
.datalist {
position: relative;
display: inline-block;
font-family: gotham;
}
.datalist-content {
display: none;
position: absolute;
background-color: #f1f1f1;
width:200px;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.datalist-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.datalist a:hover {background-color: #ddd;}
/*END Database drop down*/
/*START Criteria box */
.dropcriteria {
font-family: Gotham;
color: white;
padding: 16px;
font-size: 16px;
width:200px;
border: none;
cursor: pointer;
background-color: #46b3d1;
}
.dropcriteria:hover, .dropcriteria:focus {
background-color: #22819b
}
.criterialist {
position: relative;
display: inline-block;
font-family: gotham;
}
.criterialist-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
width:200px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.criterialist-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.criterialist a:hover {background-color: #ddd;}
.show {display: block;}
/*END Criteria box */
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Project Eric</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<h1>Project Eric</h1>
<div class="datalist">
<button onclick="myFunction()" class="dropdata">Choose data table</button>
<div id="myDatalist" class="datalist-content">
<a href="oxford_ann">Oxford Annual</a>
<a href="eng_counties">English Counties</a>
<a href="oxford_qu">Oxford Quarterly</a>
</div>
</div>
<div class="criterialist">
<button onclick="criFunction()" class="dropcriteria">Choose criteria</button>
<div id="myCriteria" class="criterialist-content">
<a href="index">Index</a>
<a href="database">Database</a>
<a href="filter">Filter</a>
</div>
</div>