Issue with top-level navigation menu dropdown functionality

I am experiencing an issue with a dropdown menu that has top level and two sub levels. The problem is that while the sub levels function correctly when clicked, the top level does not navigate to the intended page upon clicking.

var menu_Sub = $(".menu-has-sub");
var menu_Sub_Li;
$(".mobile-device .menu-has-sub").find(".fa:first").removeClass("fa-angle-right").addClass("fa-angle-down");
menu_Sub.click(function() {
  if ($(".header").hasClass("mobile-device")) {
    menu_Sub_Li = $(this).parent("li:first");
    if (menu_Sub_Li.hasClass("menu-opened")) {
      menu_Sub_Li.find(".sub-dropdown:first").slideUp(function() {
        menu_Sub_Li.removeClass("menu-opened");
        menu_Sub_Li.find(".menu-has-sub").find(".fa:first").removeClass("fa-angle-up").addClass("fa-angle-down");
      });
    } else {
      $(this).find(".fa:first").removeClass("fa-angle-down").addClass("fa-angle-up");
      menu_Sub_Li.addClass("menu-opened");
      menu_Sub_Li.find(".sub-dropdown:first").slideDown();
    }
    return false;
  } else {
    return false;
  }
});
menu_Sub_Li = menu_Sub.parent("li");
menu_Sub_Li.hover(function() {
  if (!($(".header").hasClass("mobile-device"))) {
    $(this).find(".sub-dropdown:first").stop(true, true).fadeIn("fast");
  }
}, function() {
  if (!($(".header").hasClass("mobile-device"))) {
    $(this).find(".sub-dropdown:first").stop(true, true).delay(100).fadeOut("fast");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="nav-menu">
  <ul class="nav-menu-inner">
    <li>
      <a href="connexion">Home</a>
    </li>
    <li>
      <a class="menu-has-sub" href="about-us">About us <i class="fa fa-angle-down"></i></a>
      <!-- Dropdown -->
      <ul class="sub-dropdown dropdown">
        <li>
          <a class="menu-has-sub" href="clients-case-studies">Clients and Case Studies</a>

        </li>
      </ul>
      <!-- End Dropdown -->

If you have any suggestions or solutions, please let me know. Thank you for your help.

Answer №1

The issue arises in the return false statement within your code:

menu_Sub.click(function () {
if ($(".header").hasClass("mobile-device")) {
    menu_Sub_Li = $(this).parent("li:first");
    if (menu_Sub_Li.hasClass("menu-opened")) {
...
}
else {
    return false;  // this prevents the default click action from happening
}
});

Essentially, what you're specifying here is that if I click on the .menu-has-sub link and it lacks a .mobile-device class, I want it to return false.

This effectively equates to event.preventDefault() - refer to this helpful explanation on Stack Overflow: event.preventDefault() vs. return false

However, this might be causing your problem. Exercise caution when preventing the default action on links if you intend for them to redirect somewhere.

Check out this fiddle with the line commented out.

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

Troubleshooting: Custom HTML Attribute in Angular 8 Component HTML Not Functioning as Expected

I have a unique situation in my Angular 8 application where I utilize custom HTML attributes and then use jQuery to fetch those attributes for various styling purposes such as setting background images and adjusting the height of div elements. Below is th ...

In need of assistance with adding a divider within a box using HTML/CSS

As a beginner, I may have messy code but I'm working on creating a job search page with a bar displaying the results like this: I've almost got it, except I can't figure out how to add that divider between PREV, 1 to 100, and NEXT. My curre ...

Exploring the power of async/await and promise in TypeScript

I'm puzzled as to why the return type string in this method is showing up as a red error: exportPageAsText(pageNumber: number): string { (async () => { const text = await this.pdfViewerService.getPageAsText(pageNumber); ...

Looking for some CSS to style the image below

Looking for CSS assistance to style the sample image provided below. The letter within the circular element is contained in a div while the subscript Z is represented by an image of a circle. https://i.sstatic.net/sKF1H.png .text-center { text-align: ...

How can I determine the specific quantity of XPATH links with unique identifiers in Selenium?

Seeking automation with Python3 and selenium to streamline searches on a public information site. The process involves entering a person's name, selecting the desired spelling (with or without accents), navigating through a list of lawsuits, and acces ...

Vuejs - Display multiple dates within one component

I have developed various components (such as tables, selects, etc) that all rely on the same methods to access API information. The main objective is to be able to use these components across different pages of the application in a way that allows for fle ...

concealing the date selection feature on the data picker

$('.year').datepicker({ changeMonth: true, changeYear: true, showButtonPanel: true, dateFormat: 'MM yy', onOpen: function(dateText, inst) { $("table.ui-datepicker-calendar").addClass('hide') }, onClos ...

How can you deactivate all form elements in HTML except for the Submit button?

Is there a method available to automatically deactivate all form elements except the submit button as soon as the form loads? This would entail pre-loading data from the backend onto a JSP page while restricting user access for editing. Users will only be ...

Mapping the changes in the checkbox of a material tree node

Check out this demo on StackBlitz: Tree View I'm having issues with the tree not displaying as desired. I would like it to look like this: Manager Sublist Manager 1 Manager 2 Manager 3 Could you please review and provide some advic ...

Having trouble getting a Mocha test to display two decimal places in Javascript? Both Big and Decimal libraries are causing issues

After encountering an error with the Big library, I decided to switch to Decimal. First, I ran npm install Decimal Then, I added the following code: const Decimal = require('decimal'); Even after following the examples, my comparison returned { ...

"Utilizing the ajaxcontroltoolkit CascadingDropDown feature to trigger a method once the control

On my webpage, I am using a CascadingDropDown control and I am looking to trigger some jQuery actions once the dropdown is populated. As per the guidance provided in the documentation, there is an event called (populated) that can be used for this purpose. ...

Aligning a set of list items horizontally within an unordered list is a great way to maintain a clean

I'm struggling with centering the alignment of li elements within a ul. Below is the excerpt from my code: ul.nav { width: 1000px; margin: 0 auto; list-style-type: none; } .nav li { float: left; width: 300px; } #government { clear: left ...

Tips for displaying a notification after a successful form submission using jQuery and AJAX

While attempting to submit a PHP form using jquery $.ajax();, I have encountered a peculiar issue. The form is successfully submitted, however, when I try to display an alert message - alert(SUCCESS); on success, it does not work as expected. Any ideas on ...

Learn the technique of storing a sequence of functions within a variable using jQuery

For instance, I am looking to assign a sequential process to multiple variables and utilize them in various scenarios. var disable_btn = true; var button_me = $('#contents.getsamplekit .sample-form .form #submit-btn-freeSample-me'); var button_d ...

"Safari is not displaying the element's height correctly when set to 100

I am facing an issue where I have a child div vertically centered under a parent div with a specific height. The child div is assigned the height:100% property, but it seems to be not working correctly on Safari only. To see more about this problem, you ca ...

AngularJS scope watch isn't firing as expected

In the scope, I store a filtering string for dates. $scope.myModel = {date:""}; Utilizing a jQuery datepicker, <input date-value="myModel.date" date-picker /> This directive updates the string through AngularJS - Attribute directive input value c ...

After successfully building with Vite, an error occurs stating "TypeError: can't convert undefined to object." However, during development with Vite, everything functions flawlessly

Currently, I am utilizing Vite in conjunction with React and Typescript for my project. Interestingly, when I execute 'vite dev', the live version of the website works flawlessly without any errors showing up on the console. However, things take ...

A versatile CSS solution for fixed background images that stretch to fit any screen height across multiple browsers

Similar Question: Stretch and Scale CSS Background I am interested in finding a method to create a background with specific criteria: - remain fixed in place - adjust proportionally based on the window's height - work across all browser types or ...

Attempting to utilize jQuery for altering the background URL leads to the unexpected removal of the -webkit-background-clip:text; effect

Can the -webkit-background-clip:text; effect be preserved when using jQuery to switch the background url? I am experiencing issues where changing the URL removes the text clipping effect. jQuery(function($) { var whatToSwitch = $('.homepage_ove ...

Issue with .css() function failing to apply width attribute

My goal is to shift the width of a div as specific images load in my application. I have tried reading the div's current width, adding 128 to it, and then using the .css() function to update the div's new width. However, I have encountered an iss ...