AreWe

Dropdown menu

Oct 5th, 2020
1,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. solve = () => {
  2.     const box = document.getElementById('box');
  3.     const ul = document.getElementById('dropdown-ul');
  4.     document.getElementById('dropdown').addEventListener('click', () => {
  5.         const display = ul.style.display === 'block' ? 'none' : 'block';
  6.         ul.style.display = display;
  7.         if (display === 'none') {
  8.             box.style['background-color'] = 'black';
  9.             box.style['color'] = 'white';
  10.         }
  11.     });
  12.     [...ul.querySelectorAll('li')].forEach((li) => {
  13.         li.addEventListener('click', () => {
  14.             box.style['background-color'] = li.textContent;
  15.             box.style['color'] = 'black';
  16.         });
  17.     });
  18. };
Advertisement
Add Comment
Please, Sign In to add comment