Troubles with concealing dropdown menu when hovering

I have noticed that this issue has been raised before, but none of the solutions provided seem to fix my problem specifically.

The submenu in my menu is not functioning as intended. It should be displayed when hovered over and hidden when not being hovered on.

Currently, I can hover over the menu item, but it disappears when I try to select the next option.

I can't figure out where I'm making a mistake. This feature seems so simple, yet it's driving me crazy! Below is the code I am working with:

$(document).ready(function() {
  $('.dropdown-submenu a.subhover').on("mouseover", function(e) {
    $(this).next('ul').toggle();
    e.stopPropagation();
    e.preventDefault();
  });
});

$(document).ready(function() {
  $('.dropdown-submenu a.subhover').on("mouseleave", function(e) {
    $(this).next('ul').toggle();
    e.stopPropagation();
    e.preventDefault();
  });
});
.dropdown-submenu {
  position: relative;
}

.dropdown-submenu .dropdown-menu {
  top: 0;
  left: 100%;
  margin-top: -1px;
  display: hidden;
}

.dropdown-submenu:hover .dropdown-menu {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="dropdown menu-btn">
  <a id=t estmanage class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
    <i class="fa fa-database fa-lg "></i> Manage <span class="caret"></span></a>
  <ul class="dropdown-menu">
    <li><a id="btn_addnew" href="#">Create Audit</a></li>
    <li><a href="store_departments_list.php">Departments</a></li>
    <li><a href="store_product_management2.php">Products</a></li>
    <li class="dropdown-submenu">
      <a class="subhover" tabindex="-1" href="#"> Data Entry <span class="caret"></span></a>
      <ul style="display: none;" class="dropdown-menu">
        <li><a href="labour_costs.php"> Labour Costs </a></li>
        <li><a href="purchases_list.php"> Purchases </a></li>
        <li style="cursor: pointer;"><a data-toggle="modal" data-target="#salesModal">Sales</a></li>
        <li><a href="waste_list.php"> Wastage </a></li>
      </ul>
    </li>
  </ul>
</li>

I apologize for the messy formatting of the HTML section. I cannot seem to replicate how it appears in my editor!

Answer №1

To achieve this effect, you can simply use CSS only. Here is a sample solution:

.dropdown-submenu .dropdown-menu {
  top: 0;
  left: 100%;
  margin-top: -1px;
  display: none;
}

.subhover:hover ~ .dropdown-menu, .dropdown-menu:hover {
  display: block;
}
<li class="dropdown menu-btn">
  <a id=t estmanage class="dropdown-toggle" href="#" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false>
            <i class="fa fa-database fa-lg "></i> Manage <span class="caret"></span></a>
  <ul class="dropdown-menu">
    <li><a id="btn_addnew" href="#">Create Audit</a></li>
    <li><a href="store_departments_list.php">Departments</a></li>
    <li><a href="store_product_management2.php">Products</a></li>
    <li class="dropdown-submenu">
      <a class="subhover" tabindex="-1" href="#"> Data Entry <span class="caret"></span></a>
      <ul class="dropdown-menu">
        <li><a href="labour_costs.php"> Labour Costs </a></li>
        <li><a href="purchases_list.php"> Purchases </a></li>
        <li style="cursor: pointer;"><a data-toggle="modal" data-target="#salesModal">Sales</a></li>
        <li><a href="waste_list.php"> Wastage </a></li>
      </ul>
    </li>
  </ul>
</li>

Check out this fiddle demonstration where both dropdowns work harmoniously: https://jsfiddle.net/thau2g9j/13/

Answer №2

There seems to be an issue with the code because it is executing the same lines of code on both mouseover and mouseleave events. The unnecessary code block can be commented out for testing purposes to check if it meets your requirements.

$(document).ready(function() {
  $('.dropdown-submenu a.subhover').on("mouseleave", function(e) {
    $(this).next('ul').toggle();
    e.stopPropagation();
    e.preventDefault();
  });
});

Here is a snippet of the code in action:

$(document).ready(function() {
  $('.dropdown-submenu a.subhover').on("mouseover", function(e) {
    $(this).next('ul').toggle();
    e.stopPropagation();
    e.preventDefault();
  });
});

//$(document).ready(function() {
 // $('.dropdown-submenu a.subhover').on("mouseleave", function(e) {
   // $(this).next('ul').toggle();
   // e.stopPropagation();
   // e.preventDefault();
 // });
//});
.dropdown-submenu {
  position: relative;
}

.dropdown-submenu .dropdown-menu {
  top: 0;
  left: 100%;
  margin-top: -1px;
  display: hidden;
}

.dropdown-submenu:hover .dropdown-menu {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="dropdown menu-btn">
  <a id=t estmanage class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
    <i class="fa fa-database fa-lg "></i> Manage <span class="caret"></span></a>
  <ul class="dropdown-menu">
    <li><a id="btn_addnew" href="#">Create Audit</a></li>
    <li><a href="store_departments_list.php">Departments</a></li>
    <li><a href="store_product_management2.php">Products</a></li>
    <li class="dropdown-submenu">
      <a class="subhover" tabindex="-1" href="#"> Data Entry <span class="caret"></span></a>
      <ul style="display: none;" class="dropdown-menu">
        <li><a href="labour_costs.php"> Labour Costs </a></li>
        <li><a href="purchases_list.php"> Purchases </a></li>
        <li style="cursor: pointer;"><a data-toggle="modal" data-target="#salesModal">Sales</a></li>
        <li><a href="waste_list.php"> Wastage </a></li>
      </ul>
    </li>
  </ul>
</li>

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

Utilizing jQuery and CSS to make an entire div clickable, and activate an 'a' tag hover state upon hovering

Looking to create a clickable link for the .wrapper div that directs users to the location of a.icon. Want the .wrapper div to activate the a.icon:hover state when hovered over, not just when hovering over the icon itself. Any assistance would be highly a ...

PHP seems to be resistant to receiving data from ajax requests

I am attempting to develop a drag and drop file upload feature without using a traditional form, utilizing JavaScript's FormData. However, I am encountering an issue where PHP does not seem to be receiving the uploaded file. Could there be some missin ...

AngularJS: Utilizing nested ng-repeats for dynamic data rendering

Currently, I am utilizing the ng-repeat directive in conjunction with the ng-repeat-start/ng-repeat-end options. Within the main loop, there is an additional inner ng-repeat. However, the problem lies in the fact that the outer ng-repeat-start declaration ...

Type parameter in express.js route

If I have this specific route in my Express.js server: router.get('/ad/:id', (req, res) => { const { id } = req.params Ad.getAd(id, (err, resp) => { if(err){ return handleError('Failed to load an ad' ...

Following the parsing of the list of months, the sequence undergoes a

After receiving the JSON encoded object from the server side in PHP MONTHLY_FORMAT, I am reading that object in jQuery as: var MONTHLY_FORMAT = '<?php echo $MONTHLY_FORMAT; ?>'; When checking the console output, it looks like this: {"0 ...

Error: Unable to access the value property of a null object (React/JS/TS)

I created a function that dynamically determines the background color based on a specific value. const backgroundColorResolver = () => { allQuestions.map((aq) => { if (aq.averageAnswerValue <= 4) return "#EE7362"; if (a ...

Toggle the visibility of images with input radio buttons

Explanation I am attempting to display an image and hide the others based on a radio input selection. It works without using label, but when I add label, it does not work properly. The issue may be with eq($(this).index()) as it ends up selecting a differ ...

send another response following the completion of the request.end()

I am facing challenges with writing the correct callback function. The situation involves a user making a request to "/city", which then triggers the server to make a request to a third-party service for data retrieval using http.request(). I successfully ...

Image encoded in base64 not appearing on the screen

Hey there, I'm currently working on implementing a jQuery image uploader that generates a base64 code when an image is selected. function readImage(input) { if (input.files && input.files[0]) { var FR= new FileReader(); FR.onload ...

jQuery is not active on responsive designs

I have implemented a script in my JavaScript code that changes the color of the navigation bar when scrolling. The navigation bar transitions to a white color as you scroll down. However, I am facing an issue with responsiveness and would like to deactivat ...

the present time plus a one-hour situation

I am facing a challenge where I need to dynamically adjust the path based on specific time conditions. In this situation, I will be working with two date variables retrieved from an API: CheckInStartDate and CheckInEndDate. The current system date and tim ...

When would this be required within JavaScript or Angular?

Being new to JavaScript, I have come across information stating that the value of this changes based on how a function is invoked. However, I am confused about when it is necessary to use this and when it is not. Some code examples I've seen utilize t ...

API requests seem to be failing on the server side, yet they are functioning properly when made through the browser

My current project involves utilizing an API that provides detailed information about countries. I've set up an express server to handle requests to this API, but for some reason it's not making the request. Interestingly, when I directly access ...

Splitting elements into two categories with Angular.JS: Comparing ng-hide and filter

My task is to take an object with data and display it in two separate lists. The structure of the object is as follows: var data = [ {name: "Something 1", active: 1, datetime: "goes", author: "here"}, {name: "Something 2", active: 0, datetime: "goes ...

To combine arrays A and B within a React useState object, simply pass them as arguments when initializing the state

Currently, I am working on integrating infinite scroll functionality in React, where I need to fetch data from a backend API for loading content on the next page. The challenge I'm facing is that my state is set as an object using useState, and I need ...

-=:Heading with a decorative vertical line=:-

Currently, I am working on a CSS project that requires page titles (headings) to be centered with horizontal lines positioned vertically on both sides. Additionally, there is a background image on the page which means the title's background must be tr ...

Using jQuery to reveal and conceal elements upon hover interaction

I have implemented a basic jquery function to display and hide a div when hovering over an anchor. However, the issue I am facing is that the div disappears when I move my cursor away from the anchor tag. I want to be able to interact with the div without ...

Preventing Users from Uploading Anything Other than PDFs with Vue

I am currently working with Bootstrap-Vue and Vue2. Utilizing the Form File Input, I want to enable users to upload files, but specifically in PDF format. To achieve this, I have included accept="application/pdf": <b-form-file v-model=&quo ...

Challenge in backward compatibility when converting from .on() to .live()

Struggling to make hammer.js work with an outdated 1.6 jQuery in my CMS. The function "on()" isn't available, so I have to use "live()". Here are the two instances: 1. var hammertime = new Hammer(element[0], { drag_lock_to_axis: true }); hammertime. ...

The attempt to test the AngularJS application using the Jasmine plugin was unsuccessful

I'm currently in the process of learning how to write test cases for my angularJS application. As a beginner, I'm searching for some sample examples of working demos. I came across an example online that uses the Jasmine plugin to test an angular ...