Upon clicking the navigation link, the dropdown menu is opening and closing simultaneously

My dropdown menu is behaving strangely, opening and closing simultaneously when I click the nav link. I've been working on creating a dropdown menu, but it keeps closing on its own:

Whenever I try to open the dropdown, it closes by itself, and I'm having trouble identifying the issue. Here are the HTML, CSS, and JavaScript codes that I've used:

Check out this image for reference

let dropdownLinks = document.querySelector(".nav-linkdrp");
let drpdwnMenu = document.querySelector(".drpdwn-menu");

function drpdwnMenuFunc() {
  drpdwnMenu.classList.toggle("drpdwn-menu-active");
}
.navbar-nav {
  display: flex;
  gap: 2vw;
}

.nav-items {
  list-style: none;
}

.drpdwn {
  display: flex;
  gap: 0.4vw;
  align-items: center;
  justify-content: center;
}

.drpdwn-menu {
  display: none;
}

.drpdwn-menu-active {
  z-index: 99;
  display: flex;
  position: absolute;
  top: 100%;
  flex-direction: column;
  align-items: center;
  background-color: white;
  width: 10vw;
  height: auto;
  border: solid black 3px;
  padding: 20px 20px;
}

.drpdwn-item {
  list-style: none;
  padding: 1vh 2vw 1vh 2vw;
}

.drpdwn-item a {
  text-decoration: none;
  font-weight: 500;
  color: black;
}

.nav-links {
  text-decoration: none;
  color: black;
  font-weight: 500;
  font-size: 20px;
}
<ul class="navbar-nav">
  <li class="nav-items"><a href="" class="nav-links">HOME</a></li>
  <li onclick="drpdwnMenuFunc()" class="nav-items drpdwn">
    <a href="" class="nav-links nav-linkdrp">LEARN</a>
    <i class="ri-arrow-down-s-fill"></i>
    <ul class="drpdwn-menu">
      <li class="drpdwn-item"><a href="">ABOUT</a></li>
      <li class="drpdwn-item"><a href="">FAQ</a></li>
      <li class="drpdwn-item"><a href="">BLOGS</a></li>
    </ul>
  </li>
  <li class="nav-items drpdwn">
    <a href="" class="nav-links">DISCOVER</a>
    <i class="ri-arrow-down-s-fill"></i>
  </li>
  <li class="nav-items"><a href="" class="nav-links">CONTACT</a></li>
</ul>

I had hoped for smooth functioning of my dropdown menu.

Answer №1

The issue at hand is deceptively simple.

You inadvertently included an anchor tag that triggers a page reload when clicked.

To resolve this, simply swap out:

<a href="" class="nav-links nav-linkdrp">LEARN</a>

with
<span class="nav-links nav-linkdrp">LEARN</span>

Furthermore, I reviewed your CSS code and recommend adding position: relative; to your .drpdwn selector.

.drpdwn {
            display: flex;
            gap: 0.4vw;
            align-items: center;
            justify-content: center;
            position: relative;
        }

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

Display multiple selection using JavaScript based on the element ID

I have a task to display the selected id using JavaScript, but currently, only the first select id is shown. Here is my HTML and JavaScript code: <tr> <td> <select name="jens_id[]" id="jens_id" required="" > <option ></op ...

Loading data into a secondary dropdown list depending on the choice made in the primary dropdown list using MVC2

I encountered a similar issue as detailed in this question and attempted to implement the suggested solution. However, I struggled to get it to work, likely due to my limited understanding of jquery and javascript. Upon inspecting it in Firebug, I noticed ...

Step-by-step guide on resolving the issue of an ajax form redirecting to a mailer

I have been working on implementing the contact form on an HTML page. The validation was functioning correctly until I added it, and now the form is no longer submitting properly. Despite this issue, the page still redirects to mailer.php. The AJAX functio ...

Codeigniter dropdown sending incorrect value to controller

When I choose an option from the dropdown list and click the submit button in the modal, the selected value is not being posted to the controller as expected. Below is my view code: <div class="form-group"> <div class="col-md-4"> ...

"Streamlining data entry with an uncomplicated HTML form input that accepts multiple values from a

As I understand it, a barcode scanner functions as nothing more than a keyboard that transmits keycode 13 after each scan. My task is straightforward: I have a basic form with only one input field and I want the ability to scan numerous barcodes and then c ...

Attempting to assign an active class to a menu item

I've been struggling for 2 hours to create a dynamic menu, but unfortunately, I haven't had any success. That's why I decided to seek some assistance. I have an HTML menu along with some JS and CSS... $(function() { var pgurl = window.l ...

Hide the div only if a specific element is found on the page

I am currently exploring different methods to show or hide a left-hand navigation menu on my Volusion store. Is it possible to use JavaScript or jQuery to alter the CSS display property of a div based on the presence of another element on the page? The i ...

Alert Div from Bootstrap fails to appear during the second ajax request if the first ajax request is canceled

I've implemented a dismissible alert in my HTML file that starts off hidden. Here's the code snippet: <div class="alert alert-dismissible" role="alert" id="msgdiv" style="margin-bottom:5px;display: none;"> <button type="button" clas ...

AJAX - Implementing a delay in displaying AJAX results

My search function uses AJAX to retrieve data from the web-server, and I am trying to implement a fade-in animation for each search result. I want the results to load and fade in one by one with a slight delay between them. Currently, it seems like all th ...

The AngularJS promise is not resolving before the external Braintree.js script finishes loading after the HTML content has been fully

Using an external braintree.js script is necessary to generate a payment widget, and unfortunately I have no control over it. The code that needs to be included in my .html page is as follows: <div id="myClient" ng-show="false">{{myClientToken}}&l ...

How can I properly integrate multer with Node and Express in this situation?

I've been working on setting up a route for uploading photos, but after making some changes, it has stopped functioning and I'm not sure how to fix it. const multer = require('multer'); // MULTER STORAGE const multerStorage = multer.di ...

Calculating the sum of fields added dynamically using jQuery

I am currently working on a dynamic table where I can add rows dynamically. However, I am facing an issue in including a subtotal at the bottom of the table. This is what I have tried so far: Here is the JavaScript code snippet: $(document).on('chan ...

Retrieving current element in AngularJS using jQuery

I have 4 templates, each with mouse actions that trigger functions: ng-mouseover="enableDragging()" ng-mouseleave="disableDragging()" Within these functions, I update scope variables and would like to add a class using jQuery without passing any paramete ...

Creating divs that adapt to different screen sizes without relying on flexbox

Currently, I am in the process of developing a chat interface where the viewport height is set to 100vh. The layout consists of a header/navbar, a "main content" section, and a footer housing the input field (you can view the code here) The way I have str ...

Retrieving Information from JSON Dataset

Having trouble retrieving data from this Json Data source: No errors are being displayed, but the requested data is not showing up. The issue lies in the response received from the API being an object instead of an array. What should be used to replace th ...

Achieving vertical and horizontal centering of content using tailwind.css in React with Next.js

Struggling to align an svg component at the center of a parent div element both vertically and horizontally using tailwind.css in a React project. Seeking feedback on my code below to identify any mistakes. The current issue, as shown in the included png ...

Are there any methods I can employ to maintain the traditional aesthetic while incorporating modern elements into web forms?

My current webforms application was built using visual basic, but now the user wants to migrate it to a new UI with Bootstrap and modern styling. The goal is to apply a side navigation bar and upper navigation bar while preserving the old pages and their o ...

Steps for setting up node-openalpr on a Windows 10 system

While attempting to install npm i node-openalpr, an error is occurring: When using request for node-pre-gyp https download, I encounter a node-pre-gyp warning and error message. The package.json for node-openalpr is not node-pre-gyp ready, as certain pr ...

I'm experiencing difficulties in installing dependencies for my project through npm, as it keeps showing an error

Recently, I decided to delve into the world of HTML5 and javascript games by running a browser game from Github. The game that caught my eye is called BrowserQuest, and you can find it on Github. A hiccup arose while using npm debug: 237 error 404 Not ...

What is the best way to position a semi-circular donut graph in the center?

I am attempting to showcase a doughnut or semi-circle chart within a materialize card, which is a responsive div element. My goal is to present simple data and utilize the chart as a progress bar. I took inspiration from the example provided in the HighCh ...