Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* This is the data we will be using, study it but don't change anything, yet. */
  2.  
  3. let menuItems = [
  4.   "Students",
  5.   "Faculty",
  6.   "What's New",
  7.   "Tech Trends",
  8.   "Music",
  9.   "Log Out"
  10. ];
  11.  
  12. /*
  13.   Step 1: Write a function that will create a menu component as seen below:
  14.   <div class="menu">
  15.     <ul>
  16.       {each menu item as a list item}
  17.     </ul>
  18.   </div>
  19.   The function takes an array as its only argument.
  20.   Step 2: Inside this function, iterate over the array creating a list item <li> element for each item in the array.
  21.   Add those items to the <ul>
  22.   Step 3: Using a DOM selector, select the menu button (the element with a class of 'menu-button') currently on the DOM.
  23.   Step 4: add a click event listener to the menu button. When clicked it should toggle the class 'menu--open' on the menu (your div with a 'menu' class).
  24.   Step 5: return the menu component.
  25.   Step 6: add the menu component to the DOM.
  26.  
  27. */
  28.  
  29. function createMenu(items) {
  30.   const div = document.createElement("div");
  31.   const ul = document.createElement("ul");
  32.   let li = null;
  33.  
  34.   items.forEach(item => {
  35.     li = document.createElement("li");
  36.     li.textContent = item;
  37.     ul.append(li);
  38.   });
  39.  
  40.   div.append(ul);
  41.  
  42.   div.classList.add("menu");
  43.  
  44.   const menuImg = document.querySelector(".menu-button");
  45.   menuImg.addEventListener("click", e => {
  46.     e.stopPropagation();
  47.     div.classList.toggle("menu--open");
  48.   });
  49.  
  50.   return div;
  51. }
  52.  
  53. const header = document.querySelector(".header");
  54. header.append(createMenu(menuItems));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement