Navigation Bar Dropdown Menu Not Responding to Clicks

I'm having trouble implementing a dropdown menu in my navigation bar that should appear when the user clicks and disappear when they click anywhere outside of it, similar to Facebook's dropdown menu with options like logout. However, for some reason, the dropdown isn't functioning as expected. I've searched extensively on StackOverflow and other online resources but haven't been able to find a solution. I would like to achieve this using only CSS and JavaScript because I'm not familiar with jQuery or other languages.

Here is the link to my code on JSFiddle: https://jsfiddle.net/8ahy32yn/9/

The HTML and CSS code snippets I've implemented are shown below:

HTML

<div id="navbar">
<ul>
   <li>
      <a href="movies.php">Home</a>
   </li>
   <li>
      <a href="faq.php">FAQs</a>
   </li>
   <li class="user" style="float:right;">
      <a href="#" class="dropbtn" onclick="UserDropdown()">Dropdown</a>
      <ul id="UserContent" class="user-content">
          <li>
            <a href="profile.php">Profile</a>
          </li>
          <li>
            <a href="gifts.php">My Gifts</a></li>
          <li>
            <a href="logout.php">Logout</a>
          </li>
      </ul>
   </li>
</ul>

CSS

ul 
{
    list-style-type: none;     
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: black;  
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 55px;
}

li
{
  float: left;
}

li a
{
  text-decoration: none;
  display: block;
  padding: 20px 25px;
  color: white;
  text-align: center;
}

li a:hover
{
  background-color: #333333;
}

.user
{
  position: relative;
  display: inline;
}

.dropbtn
{
  cursor: pointer;
}

.user-content 
{
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 100px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.user-content a 
{
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.user-content a:hover 
{
  background-color: #F1F1F1;
}

.show 
{
  display:block;
}

JavaScript

function UserDropdown() {
    document.getElementById("UserContent").classList.toggle("show");
}

window.onclick = function(event) 
{
  if (!event.target.matches('.dropbtn')) 
  {
    var dropdowns = document.getElementsByClassName("user-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) 
     {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show'))
      {
        openDropdown.classList.remove('show');
      }
    }
  }
}

Unfortunately, the dropdown doesn't show up in the Fiddle when clicked, although it does work on my localhost as intended:

https://i.stack.imgur.com/4a24u.png

Any assistance with this issue would be greatly appreciated. Thanks! :)

Answer №1

Do you need something similar to this? The desired functionality is already working, I just added some CSS for better styling.

.show 
{
  display:block;
  width: 245px; 
  right: 0; 
  left: auto;
  line-height: 30px;
  overflow: hidden
}

By adding height: auto to the ul and removing overflow: hidden from the parent of ul, you can achieve the desired result: Check it out here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for managing the onloadedmetadata event

If a video file cannot be played by the browser due to format or codec issues, I want to notify the user about it. When the browser is unable to play a video (because of unsupported format or codec), the onloadedmetadata event does not occur. I have some ...

jQuery loops through form fields and sets them as disabled

Trying to solve a question... In a form with multiple inputs, I only need to loop through the ones inside the div tagged player. <div class="player"> <input type="text" value="0" class="unit" /> <input type="text" value="0" class="unit" ...

tips for creating a mobile-friendly responsive button

I have been scouring the internet for a solution to this issue. Essentially, I am trying to position the button on the right side in desktop view. Here is an example: chrome view However, when the site is resized or viewed on mobile devices, the button sh ...

Is there a way to have content update automatically?

After writing this block of code, I discovered that clicking on one of the circles activates it and displays the corresponding content. Now, I am looking for a way to automate this process so that every 5 seconds, a new circle gets activated along with its ...

Tips for creating a horizontally scrolling div

Here is the CSS for the div that needs to be able to scroll horizontally: .form-holder{ padding-left: 20px; width: auto; background: ; overflow-x: auto; max-height: 300px; padding-bottom: 30px; } Every ...

Tips for connecting 2 Bootstrap 5 carousels

I currently have 2 Bootstrap 5 carousels (carousel-a & carousel-b) on the same page and I want them to be synchronized with each other. I believe this can be achieved using JavaScript, but I am not very familiar with it. Below is the structure of carousel- ...

How can I collect various values from input fields that are within a foreach loop using jQuery?

<script> $("#submit").click(function(e){ e.preventDefault(); subjectID = $("#subjectID").val(); subject = $("#subject_"+subjectID).val(); subject_code = $("#subject_code_"+subjectID).val(); internal_marks = ...

Updating the state of a nested array using React Hooks

After spending some time working with React Hooks, my main struggle has been dealing with arrays. Currently, I am developing a registration form for teams. Each team consists of a list of players (an array of strings). The goal is to allow users to add t ...

Is it possible to achieve this shaded gradient effect using CSS3?

Check out this image for reference. I've managed to style a horizontal rule using SVG as the background image, creating a specific effect. However, I am struggling to achieve the same result using CSS3 alone. If there are any CSS experts who can adv ...

Unable to add or publish text in CKEditor

In my ASP.NET MVC application, I am struggling to post the updated value from a CKEditor in a textarea. Here is the code snippet: <textarea name="Description" id="Description" rows="10" cols="80"> This is my textarea to be replaced with CKEditor ...

Looking to update the URL from another component?

My current project is using Angular 6 and I am working on creating a list of buttons on the left panel such as "Ice cream", "Pop corns", and more. The goal is for users to click on these buttons which will then change the URL of the add button located in t ...

Desktop sharing in HTML5/H.264 format for optimal performance and compatibility across all

Looking to showcase my desktop through live streaming over http to multiple users. The initial plan is to provide a real-time read-only view of the desktop for various users. In the future, we may consider granting users access to control the desktop using ...

The dialog box in CSS is extending too far down past the bottom of the screen, making it impossible to scroll and click on the buttons located

I am currently working on creating a dialog box with a text area using material UI. Unfortunately, when I input a significant amount of text, the dialog box ends up extending beyond the screen, making it impossible to scroll down to access the buttons. &l ...

Guidelines for utilizing the php mail() function

It seems that there is an issue with this code when running it on a local host versus a live server. I am attempting to use this code for a mail function, which works on another site but not on this one. I know the code will produce an error on localhost ...

Refining to Showcase One Menu Selection

I am having an issue with my bootstrap menu that includes a search field. The problem is that when I try to filter the dropdown menu using the search field, it filters all dropdown items instead of just the one I want. For example, if I search for a term f ...

Need to obtain the stack trace from the catch block in a request-p

Currently, I am utilizing the 'request-promise' library in my node-js application for making API calls. However, I am facing challenges in obtaining the correct call stack from the 'catch' function. Upon experimenting with it, I discove ...

I am noticing multiple quantities of the same item being added to my shopping

Recently, I encountered a problem with my online shop that seems to be related to the Javascript. The issue arises when I add an item to my shopping cart from this particular page: . Initially, when I click 'Add to Cart,' the item is correctly ad ...

Enhancing the bottom border of an unordered list when incorporating styled bullets

I have a list with some items. I used a CSS trick to style the bullet as a circle. However, I would like to add a border underneath each item to separate them. Because I removed the default bullets using list-style: none, the "bullet" is positioned -1em, ...

Dispatch in React Redux is not intercepted

I've been grappling with this issue for hours and still can't seem to find a solution. When I click the button, the function "getLocation" is being triggered (Confirmed it) However, the dispatch is not getting passed to the reducer. After r ...

Organize data that is deeply nested

Struggling to normalize deeply nested API response data? Look no further than https://github.com/paularmstrong/normalizr. This tool can help simplify your data structure. For example, if your input data looks like this: const data = [{ id: 'compone ...