Simple Dropdown List in JavaScript

Simple Dropdown List in JavaScript
Code Snippet:Simple dropdown
Author: Marcos Vinicius de Oliveira Pereira
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 2,869
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to create a simple dropdown list with icons. It converts the unordered lists to toggle dropdown navigation. You can integrate this to create a simple dropdown navigation menu.

How to Create a Simple Dropdown List in JavaScript

1. First of all, create the HTML structure as follows:

  <div class="container">
  <div class="dropdown">
    
    <span class="label">Account</span>    
    <span class="burgerbutton fa fa-bars"></span>
    
    <ul class="items-list">
        <li class="item">Profile <span class="iconitem fa fa-user"></span></li>
        <li class="item">Calendar <span class="iconitem fa fa-calendar"></span></li>
        <li class="item">Console <span class="iconitem fa fa-terminal"></span></li>
    </ul>
  
  </div> 
</div>

2. After that, add the following CSS styles to your project:

@import url("https://fonts.googleapis.com/css?family=Inconsolata");
@import url('https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
body { padding: .5rem; background: #f78a78; font-family: Inconsolata; } .container { margin: 0 auto; max-width: 250px; } .dropdown { box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24); border-radius: 3px; display: flex; position: relative; justify-content: space-between; color: #5a3b34; box-sizing: border-box; font-weight: bold; border: 0; padding: 1rem; background: #fff; } .dropdown > .burgerbutton { cursor: pointer; } .items-list { box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24); width:250px; border-radius: 3px; position: absolute; background: #fff; padding: .5rem 0; top: 3.5rem; left: 0; list-style: none; display: none; } .items-list > .item { display: flex; box-sizing: border-box; justify-content: space-between; padding: 1rem; } .items-list > .item:hover { background: #f9ada0; color: white; } .items-list > .item:hover > .iconitem { color: white; } .items-list.open { display: block; } .items-list::after { content: ''; position: absolute; right: 15px; top: -10px; width: 0; height: 0; border-left: 10px solid transparent; border-right: 10px solid transparent; border-bottom: 10px solid #fff; clear: both; }

3. Finally, add the following JavaScript code and done.

window.addEventListener('DOMContentLoaded', () => {

  const $dropdown = document.querySelector('.dropdown');
  const $burgerButton = $dropdown.querySelector('.burgerbutton');
  const $containerItems = $dropdown.querySelector('.items-list');

  $burgerButton.addEventListener('click', ($event) =>
  $containerItems.classList.toggle('open'));

  document.addEventListener('click', ($event) =>
  $event.target !== $burgerButton &&
  $containerItems.classList.remove('open'));

});

That’s all! hopefully, you have successfully integrated this dropdown list code snippet into your project. If you have any questions or are facing any issues, please feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X