Dropdown menu does not appear upon hovering over an item

I have been trying to create a menu that appears when hovering over #navigation-region. Below, you can see the HTML, CSS, and JavaScript code:

The JavaScript is simply for adding the active class on the #navigation-region element. In the CSS, there is a section for the active class that will display the previously hidden menu.

<div> 
 <div class="support-section" class="nav-secondary__support-link">
                <ul class="nav__support-links">
                    <li id="support-phone-usa" class="support-phone">
                        <a href="tel:asdasdas" ><img src="h" alt="Phone Icon">aasdasd6</a>
                    </li>
                    <li id="support-phone-int" class="support-phone">
                        <a href="asdasdasd" >Iasdasdasd</a>
                    </li >
                    <li id="navigation-region" class="">
                        <a href="#" onclick="return false;">Region: USA/Intl
                            <span><img src="" alt="icon arrow" id="arrow"></span>
                        </a>
                    </li>
                        <ul class="nav-secondary__dropdown nav-secondary__dropdown-region">
                            <li class="nav-secondary__dropdown-link nav-secondary__dropdown-title">Select your preferred<span>country / region:</span></li>
                            <span class="nav-secondary__hr"></span>
                            <li class="link nav-secondary__dropdown-link-region">
                                <a href="asdasdasd" lang="en-US"><picture><source srcset="/asdasdasd" media="(min-width: 768px)"></picture>USA + International</a>
                            </li>
                            <li class="link nav-secondary__dropdown-link-region">
                                <a href="asdasdasd/" lang="en-CA"></picture>Canada</a>
                            </li>
                            <span class="nav-secondary__hr-end"></span>
                            <li class="nav-secondary__dropdown-link nav-secondary__dropdown-copy" style="font-style: normal;text-transform: none;font-size: 14px;">Experience local shipping options and optimized product inventory for your region.</li>
                        </ul>
                    </li>
<div>
.support-section {
            display: flex;
            justify-content: flex-end;

            .nav__support-links {
                display: flex;
                list-style: none;
                align-items: center;
                gap: 20px;

                .nav-secondary__support-link-active .nav-secondary__dropdown-region {
                    opacity: 1;
                    visibility: visible;
                    display: block;
                    
                }
            }

            .nav-secondary__dropdown-region {
                visibility: hidden;
                opacity: 0;
                display: none;

                ::before {
                    content: "";
                    position: absolute;
                    top: -8px;
                    right: -3px;
                    width: 0;
                    height: 0;
                    border-left: 8px solid transparent;
                    border-right: 8px solid transparent;
                }
            }             
    }
if (navigationRegion) {
  navigationRegion.addEventListener("click", function (event) {

  const linkElem = event.target.closest('a');

  if (linkElem) {
    const parentElem = linkElem.parentElement;


    if (parentElem.classList.contains('nav-secondary__support-link-active')) {
      return false;
    }

    linkElem.parentElement.classList.add('nav-secondary__support-link-active');
    linkElem.setAttribute('aria-expanded', 'true');
  }
  });


  navigationRegion.onmouseover = function () {


    if (!navigationRegion.classList.contains('nav-secondary__support-link-active')) {
      navigationRegion.children[0].setAttribute('aria-expanded', 'true')
      navigationRegion.classList.add('nav-secondary__support-link-active');
      return false;
    }
  }

  navigationRegion.onmouseout = function () {
    if (navigationRegion.classList.contains('nav-secondary__support-link-active')) {
      navigationRegion.children[0].setAttribute('aria-expanded', 'false')
      navigationRegion.classList.remove('nav-secondary__support-link-active');
      return false;
    }
  }
}

The JavaScript successfully adds the new class, but I suspect the issue lies with the styling. Even though the class is added when hovering over #navigation-region, the menu does not appear as expected.

Answer №1

HTML Troubleshooting

Your CSS and JS are error-free. The issue lies within the HTML code.

Recommendations for Improvement

  • The container div has two class attributes. Remember to separate multiple classes with a space between them.
  • The list item tag with id "navigation-region" is closed prematurely before the element you intend to style, making it inaccessible via CSS.

View the full code here:

<div>
  <div class="support-section nav-secondary__support-link">
    <ul class="nav__support-links">
      <li id="support-phone-usa" class="support-phone">
        <a href="tel:asdasdas"><img src="h" alt="Phone Icon">aasdasd6</a>
      </li>
      <li id="support-phone-int" class="support-phone">
        <a href="asdasdasd">Iasdasdasd</a>
      </li>
     <li id="navigation-region">
        <a href="#" onclick="return false;">Region: USA/Intl
          <span><img src="" alt="icon arrow" id="arrow"></span>
        </a>
        <ul class="nav-secondary__dropdown nav-secondary__dropdown-region">
         <li class="nav-secondary__dropdown-link nav-secondary__dropdown-title">Select your preferred<span>country / region:</span></li>
         <span class="nav-secondary__hr"></span>
         <li class="link nav-secondary__dropdown-link-region">
           <a href="asdasdasd" lang="en-US">
             <picture>
               <source srcset="/asdasdasd" media="(min-width: 768px)">
            </picture>USA + International
          </a>
        </li>
        <li class="link nav-secondary__dropdown-link-region">
          <a href="asdasdasd/" lang="en-CA">Canada</a>
        </li>
        <span class="nav-secondary__hr-end"></span>
       <li class="nav-secondary__dropdown-link nav-secondary__dropdown-copy" style="font-style: normal;text-transform: none;font-size: 14px;">Experience local shipping options and optimized product inventory for your region.</li>
      </ul>
     </li>
   </ul>
  </div>
</div>

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

retrieve the identification number associated with a specific value

How can I retrieve the value of this ID, which is sent from the controller and displayed in the table? https://i.sstatic.net/UbdxT.png This is my HTML code: <tbody class="no-border-x"> <tr> <td id="id"></td> <td i ...

Unable to adjust the position of the logo image

I cannot figure out how to center the logo over a video background. Any suggestions on what I might be doing wrong? I've tried changing the "position" to relative and absolute, but without any success. #hero video { width:100%; height:100%; p ...

Determine if each element in an array is present in the MongoDB document's array

Is it possible to determine whether a specific element exists within an array in a MongoDB document? For instance: If we have the following document { "_id": "A", "userId": "B", "selectedUsers": ["C", "D"] } and another array ["E", "C"]. ...

How can I combine an ordinary image and a CSS2D image in THREE.js CSS2DRenderer and save them together as a single .jpg file?

After implementing the following code... (1) I successfully saved regular rendered THREE.js scenes as .jpg files; (2) Additionally, I managed to utilize CSS2DRenderer to display CSS2D labels on top of the canvas; (3) Now, my goal is to save the image wi ...

Why does the "margin" CSS style fail to function properly in FlowPanel within GWT?

I'm currently working with Gwt and have encountered a problem with styling buttons. Let's take a look at the following code snippet: .marginRight{ margin-right: 10px; } FlowPanel myFP=new FlowPanel(); Button myButton=new Button("Button"); Butt ...

Is there a way to exchange the chosen options between two bootstrap-vue multiple select boxes?

Is there a way to switch the selected options between two bootstrap-vue multiple select boxes? I am trying to create a UI similar to the image shown below but I am struggling with writing the method. This is how I have written the template: https://i.sst ...

What is the process for accessing and utilizing the value returned by a promise in a separate file?

I have developed a small project to demonstrate an issue. There are a total of 5 files in this project: - A container file that includes all the dependency injections. - A service file containing the necessary functions to be executed. - A controller fil ...

JavaScript HTML Object Manipulation and Templating System

I am searching for a JavaScript library that is capable of performing the following: var items = [1, 2]; var html = div( ul({ id: "some-id", class: "some-class" })(items.each(function(item) { return li(item); })); html == ...

A guide on organizing an array of objects by a specific property using a separate array

Here is the array I am working with: var arr = [ { count: 27, dataRil: "08/06/21", subCateg: "FISH", }, { count: 22, dataRil: "08/06/21", subCateg: "DOG", }, { count: 28, dat ...

A step-by-step guide on activating dark mode for the markdown-it-vue plugin while incorporating Vuetify styling

I'm looking to display some documentation in my Vue application. The documentation is in markdown format, so I have already integrated the markdown-it-vue plugin. However, I've run into an issue where the plugin doesn't support vuetify dark ...

When the onClick event on one element triggers a change in another element within the

I apologize if the title is not clear. I have a React component with buttons and text, which I have used three times. My goal is for each button to display its respective text when clicked, while hiding the texts associated with the other buttons. While m ...

SQL message comparison error within mysql module is malfunctioning

Currently, I am utilizing the nodejs MySQL module to automatically create a table every month. To ensure this process runs smoothly, I need to check if the table already exists using the condition err.sqlMessage == Table 'tableName' alre ...

Ways to superimpose an image

Struggling to overlay an image over two div columns - I attempted using Z-index and experimented with various positioning properties. What am I missing? The triangle situated behind the two boxes, along with the additional text on JSFiddle, should appear ...

Using Next.js to implement a timeout feature that pauses using a React state

Below is the code for my component, which is called on a page with <DetectPitch />: import { useEffect, useState } from 'react'; export default function DetectPitch() { const [detect, setDetect] = useState(false); useEffect(() => ...

Issue with ng-disabled not functioning properly for href tag within list item

Is there a way to prevent clicking on the <a> tag within a list item in a UI list so that the associated <div> is not displayed when clicked (excluding the last list item)? I have attempted using ng-disabled directly on the list item attribute ...

What is the best way to horizontally center an image with a transparent background using CSS?

A search button triggers a processing gif image to appear in the center with a transparent background. The content of this function is described below. Thank you for all responses. .img-loader{ text-align: center; width: 50px; display: block; ma ...

Conceal the content inside a DIV based on the character length of a

I'm having trouble trying to hide content in a div based on the length of a textbox value when a key is pressed. The current solution isn't working as expected. Is the backspace key not considered a keypress event? If you take a look at the code ...

Displaying search results from a PHP query in an HTML text box

Within my PHP page, I am populating a table with the results of a query using the following code: <?php $json=file_get_contents("http://www.example.com/myfile.php"); $data = json_decode($json); if (count($data->result)) { echo "<table id= &apos ...

When working with Bootstrap-Vue, what is the best way to stop a b-dropdown from closing when a nested b-input component is clicked on?

I'm struggling to grasp the concept of Vue's Event Modifiers. The documentation suggests that adding this simple code should do the trick: <!-- the click event's propagation will be stopped --> <a v-on:click.stop="doThis"& ...

Struggling to send information from an HTML/PHP form

Hello everyone, I recently completed a project using Argon (Bootstrap / vue.js). It is a Single Page Application with a contact section that includes a contact form for name, email, and message. The contact form is built within a .vue file, which is import ...