The URL in the dropdown menu on an Android device is unresponsive

I am facing an issue with the navigation menu on my website. It works perfectly fine on desktop and Apple devices, but on Android devices using Chrome, there is a problem. The submenu opens correctly, but when trying to click on a link within the submenu, it closes abruptly without redirecting to the desired page. I have tried multiple solutions found online, but haven't been successful so far. Below is the simplified version of the code. Any help would be greatly appreciated.

<nav id="nav">

<ul>

  <li>Main 1</a>
    <ul>
      <li><a href="http://www.stackoverflow.com">Submenu 1</a></li>
      <li><a href="http://www.stackoverflow.com">Submenu 2</a></li>
     </ul>
  </li>

  <li>Main 2
    <ul>
      <li><a href="http://www.stackoverflow.com">Submenu 1</a></li>
      <li><a href="http://www.stackoverflow.com">Submenu 2</a></li>
      <li><a href="http://www.stackoverflow.com">Submenu 3</a></li>
    </ul>
  </li>

     <li>Main 3
    <ul>
      <li><a href="http://www.stackoverflow.com">Submenu 1</a></li>
      <li><a href="http://www.stackoverflow.com">Submenu 2</a></li>
      <li><a href="http://www.stackoverflow.com">Submenu 3</a></li>
    </ul>
  </li>
     </ul>
</nav>

nav ul li ul {
display: none; }
nav ul li:hover > ul {
display: block; }

Answer №1

Dealing with Android has been quite a challenge for me lately. Despite my hours of searching and testing, I've come to the conclusion that achieving a consistent hover effect across all devices is nearly impossible. To address this issue, I've implemented the following solution:

  • I utilize jQuery to display submenus when the parent item is clicked. Clicking on the parent item again will hide the corresponding submenu. Only one submenu can be open at a time.
  • For users who have disabled JavaScript, I fallback to using traditional CSS hover effects.

Below is the code snippet that illustrates my approach. As a beginner in jQuery, I'm open to feedback or suggestions for more efficient solutions!

<nav>
    <ul>
      <li class="nietactief nonJSclass">Main Menu 1
      <ul>
      <li><a href="http://www.stackoverflow.com">Submenu 1a</a></li>
      <li><a href="http://www.stackoverflow.com">Submenu 1b</a></li></ul></li>
      <li class="nietactief nonJSclass">Main Menu 2
        <ul>
          <li><a href="http://www.stackoverflow.com">Submenu 2a</a></li>
          <li><a href="http://www.stackoverflow.com">Submenu 2b</a></li>
          <li><a href="http://www.stackoverflow.com">Submenu 2c</a></li>
        </ul>
      </li>
      <li class="nietactief nonJSclass">Main Menu 3
        <ul>
          <li><a href="http://www.stackoverflow.com">Main Menu 3a</a></li>
          <li><a href="http://www.stackoverflow.com">Main Menu 3b</a></li>
          <li><a href="http://www.stackoverflow.com">Main Menu 3c</a></li>
        </ul>
      </li>
    </ul>
  </nav>
nav ul li.nietactief ul {
    display: none;
}
nav ul li.actief > ul {
    display: block;
}
nav ul li.nonJSclass:hover > ul {
    display: block;
}
$( document ).ready(function() {

  $(".nonJSclass").each(function()
  {
    $(this).removeClass("nonJSclass");
  });

$('nav ul li').click(function() {

    var classActief = $(this).attr('class');

if (classActief == 'actief'){

    $(this).removeClass('actief').addClass('nietactief');
} 

if (classActief == 'nietactief'){

    $('nav ul li').removeClass('actief').addClass('nietactief');

}

if (classActief == 'nietactief'){

    $(this).removeClass('nietactief').addClass('actief');

}  


});



});



 </script> 

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

Is there a way to retrieve the immediate children of all elements using CSS selectors in Selenium?

Although I attempted to utilize the ">" syntax, selenium does not seem to accept it. I am aware that Xpath can be used to obtain what I need, however our project exclusively utilizes CSS selectors. My goal is to create a list containing only the immedi ...

Troubleshooting: @media query's max-width CSS property not functioning as expected

(The styling works on all browsers except Chrome) I am trying to apply a specific style only when the browser width is less than 1400px Unfortunately, using max-width is not producing the desired effect @media only screen and (max-width:1400px) { .head ...

Personalize Angular material mattooltip and style information for display in tooltip

I need assistance displaying data in a tooltip in JSON format similar to the image provided. Although I can load the data in the tooltip, it does not appear formatted like the attached picture. <ng-container matColumnDef="Alert"> &l ...

Enhancing your design with animated borders on hover

This unique border animation caught my eye and sparked my curiosity. I'm interested to know if it can be achieved using only HTML5/CSS for a hover effect. Have you discovered any other visually appealing hover animations worth mentioning? ...

A guide on retrieving the value of input elements using class names in jQuery

I need help fetching the values of input elements with the class "features". I am able to access all of them. alert($('.features').length); Can someone please confirm if my understanding is correct? $('.features') is an array of HTM ...

Press the div using JavaScript and jQuery

Is it possible in JavaScript to automatically click on a hidden div once it is displayed? For instance, imagine we have a div with the style "display:none;" like so: <div style="display:none;" id="button">Hello World</div> Once this div&apos ...

CSS font attribute stylus variable

Can you help me troubleshoot this issue with the variable in stylus that is setting the font attribute using shorthand syntax? button-font = 100 16px Helvetica Neue', Helvetica, Arial, sans-serif I'm encountering the following error message: ...

Creating personalized selections with the bootstrap combobox: a guide

I created a form that displays options as I type, but I want to add icons next to the dropdown items and have them link to specific pages when clicked. For example, I'd like an icon next to "alabama" that appears when I type "a" and directs me to a ce ...

Is it possible to submit two forms simultaneously using jQuery or AJAX?

My plan is to save 2 forms, with the first form providing the Foreign key for the second form. This is my attempt at saving this using JavaScript: $("#btnSave").click(function (e) { e.preventDefault(); $('#workForm').submit(); ...

What advantages and disadvantages come with choosing between using vh and vw for responsive text?

My main inquiry is centered around the various techniques for creating responsive text. Is there a particular method that stands out as the best option for general use, or do different methods serve distinct purposes? ...

Unable to modify the color of the alert notification in JavaScript using React

I've been attempting to create an alert component, but I'm facing an issue with adjusting the color of the alert message. Upon enabling dark mode in the navbar (located at the bottom of the navbar component code), an alert message confirming the ...

What is the best way to implement jQuery on an HTML file instead of relying solely on the Document Object Model

My attempt to make modifications to an HTML document has been challenging, as it differs from working with the DOM. I've realized that the syntax I typically use with the DOM doesn't translate well to this other document. For instance, while I co ...

When it comes to identifying a click outside of an element, the Jquery or Javascript function may encounter some challenges specifically with Internet Explorer

After reviewing various solutions online, I noticed that they all function properly on Chrome and Firefox but encounter issues with Internet Explorer when interacting with an SVG. For instance, consider the following code snippet: $(document).on("click",( ...

Inconsistency in retrieving and setting width values with jQuery across various web browsers

My TH element currently has both left and right padding set to 18px. I am attempting to determine its width using jQuery, convert this width from pixels to a percentage, and then set the element's width based on this calculated percentage. var origin ...

Display the item for which the date is more recent than today's date

Is there a way to display only upcoming 'events' on my page based on their event_date? <% for (let event of events){%> <div class="card mb-3"> <div class="row"> <div class="col ...

How to ensure HTML elements are styled to occupy the full width of their parent container while respecting the minimum width requirement

Is there a way to make HTML elements have a width ranging from 150px to 200px while filling up 100% of their parent's width? Check out Image 1: https://i.sstatic.net/nYNGp.jpg And here is Image 2 (after resizing the window): https://i.sstatic.net/ ...

Sudden slowdown due to Jquery error

I am encountering an issue with the Jquery registration validation. I am struggling to display the error message if a name is not filled in. jQuery(document).ready(function () { $('#register').submit(function () { var action = $(thi ...

Ensure consistent element heights within adjacent columns using CSS

My template looks like this: https://i.sstatic.net/hoLzR.jpg I am trying to keep the same height between each item in both columns, where the height is determined by the tallest item in each column when they are placed side by side. But on smaller screen ...

"Using Java HttpServlet to send data as a Gson string, then retrieving it in JavaScript as

On the server side, I have this Java code snippet: public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("application/json;charset=UTF-8"); resp.setHeader("Cache-Control", "no-cach ...

JS - reloading the location after a function has been carried out with a short delay

Currently, I am using a JS / AJAX script to update information in my table. After the script finishes running, I want to reload the page. Unfortunately, when using my current code, the page reloads instantly. Is there a way to add a delay of 3 seconds befo ...