File size: 757 Bytes
3aa64d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// let the page load...
document.addEventListener('DOMContentLoaded', () => {
  // assign the button click
  document.querySelectorAll('.dropbtn').forEach(btn => {
    btn.addEventListener('click', e => {
   
      // first close all
      document.querySelectorAll('.dropdown-content').forEach(div => div.classList.remove('show'))
      // open the one you want which is CLOSEST
      e.target.closest('.dropdown').querySelector('.dropdown-content').classList.add('show');
      
      
    })
  })

  // catch the click outside
  document.addEventListener('click', e => {
  if (e.target.classList.contains('dropbtn')) return;
    document.querySelectorAll('.dropdown-content').forEach(div => div.classList.remove('show'))
  })



})