CSS dropdown submenu is not disappearing

I need help with my submenu. I want it to appear when the cursor hovers over a menu item, and for each new menu option selected, the previous submenu should be replaced. Currently, the first submenu stays on the screen and the new one appears underneath it. Can someone assist me with this? Thank you!

HTML:

     <div id="menu">
<div class="menu" id="menu1" onmouseover="showSubMenu(this)"><a href="#.html">   MENU1 </a>
    <div class="submenu" id="submenu1" style="display:none" onmouseout="showSubMenu(this)">
        <div><a href="#.html">SUBMENU11</a></div>
        <div><a href="#.html">SUBMENU12</a></div>
        <div><a href="#.html">SUBMENU13</a></div>
        <div><a href="#.html">SUBMENU14</a></div>

    </div>
</div>
<div class="menu" id="menu2" onmouseover="showSubMenu(this)"><a href="#.html">MENU2</a>
    <div class="submenu" id="submenu2" style="display:none" onmouseout="showSubMenu(this)">
        <div><a href="#.html">SUBMENU21</a></div>
        <div><a href="#.html">SUBMENU22</a></div>
        <div><a href="#.html">SUBMENU23</a></div>
        <div><a href="#.html">SUBMENU24</a></div>
        <div><a href="#.html">SUBMENU25</a></div>

    </div>
</div>
<div class="menu" id="menu3" onmouseover="showSubMenu(this)"><a href="#.html">MENU3</a>
    <div class="submenu" id="submenu3" style="display:none" onmouseout="showSubMenu(this)">
        <div><a href="#.html">SUBMENU31</a></div>
        <div><a href="#.html">SUBMENU32</a></div>
        <div><a href="#.html">SUBMENU33</a></div>
        <div><a href="#.html">SUBMENU34</a></div>
    </div>
</div>
<div class="menu" id="menu4"><a href="#">MENU4</a></div>
<div class="menu" id="menu4"><a href="#">MENU5</a></div>
<div class="menu" id="menu5"><a href="#">MENU6</a></div>
<div class="menu" id="menu6"><a href="#">MENU7</a></div>

CSS:

    #menu{
margin:0px 0 0 9px;
background:#50626c;
color:#fff;
float:left;
display:inline;}
    #menu div{
width:234px;
text-align:center;}
     .menu{
position:relative;}
    #menu div a:link, #menu div a:visited, #menu div a:hover{
color:#fff;
font-family:arial,sans-serif;
font-size:14px;
text-decoration:none;
padding-top:7px;
height:28px;
display:block;}
    #menu div a:link, #menu div a:visited{
background:url(BG.jpg) no-repeat;}
    #menu div a:hover, #menu div a:active, #menu div a:focus{
background:#4172CB ;
text-decoration: underline;}
     .submenu{
position:absolute;
left:234px;
top:0;}

JavaScript:

function showSubMenu(obj){
    var id = obj.id;

    for(var i = 1; i <= 3; i++){
            document.getElementById('submenu'+i).style.display = "none";
    }

    if(document.getElementById('sub'+id)){
            document.getElementById('sub'+id).style.display = "block";
    }

}

Answer №1

It seems like the for loop is setting all of them and ignoring the if statement that follows. You should try placing the if statement inside the for loop and include an else statement to display other elements as none. Alternatively, you can achieve this using just CSS hovers without any JavaScript - you may want to explore this option. Take a look at how the main navigation on the following site (About/Portfolio/Contact/Extras) achieves it with CSS only:

Here's how you can modify your code:

for(var i = 1; i <= 2; i++){
    if(document.getElementById('sous'+id)){
        document.getElementById('sous'+id).style.display = "block";
    } else {
        document.getElementById('sousmenu'+i).style.display = "none";
    }
}

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

Basic JavaScript code for converting text input in an HTML textbox. Fails when attempted a second time

When using the replace method, I've noticed that only the first occurrence of "test" is being converted to "good" when I input "test test". Can anyone explain why this is happening? Also, if I wanted to replace 20 different words, would I need to crea ...

(React Native) Creating a visually appealing grid layout for displaying an array of item cards

I am working with two arrays named 'word' and 'definition' export default class Dictionary extends React.Component { constructor(props) { super(props); this.state = { word: [], definition:[], index: ...

Trouble with jQuery not loading properly in HTML

I'm experiencing an issue with a code I want to use, but it isn't responding. Even though I've verified that the code is correct because it's on jsfiddle: http://jsfiddle.net/User86745458/ztz4Lf23/ However, when I try to copy and paste ...

Switch to reveal a div that holds a different HTML page

I've been struggling to create a button that opens a div containing another HTML page. Initially, the button was working but the main page was opening with the div already visible, when I needed it hidden. Now, after making some changes, the button is ...

What is the reason for webpack searching for jQuery even though it is not necessary for the module?

Before I proceed with my question, I want to clarify that I may not fully grasp the intricacies of this issue and I apologize if it does not meet the standards of Stackoverflow's Q&A. Currently, we are facing a challenge in our project while tryi ...

Model is disregarding media queries

Using LESS for CSS compilation, I have encountered an issue with the media query in my code. Specifically, I am attempting to apply different styles for mobile devices but they are not being rendered as expected. #main-wrap header #hdr-nav nav { width: ...

How can I make the Bootstrap tooltip only appear when clicked inside a Vue Component and disappear when hovered over?

I am having a challenge with Bootstrap ToolTip in my Vue component. I only want the notice to show when the button is clicked, not on hover as it currently behaves. I attempted using jQuery within the Vue component without success. Can someone please adv ...

Avoid altering the Summernote HTML WYSIWYG editor page due to content CSS

We are currently utilizing the Summernote wysiwyg editor to review and preview html content, some of which contains external stylesheet references. This css not only affects Summernote itself but also alters the styling of the parent page where Summernote ...

communication between a Windows C# application and a web-based JavaScript interface

Similar Question: how to get variable in desktop c# program from web javascript application Encountered an issue involving two applications: a desktop application written in C# and a web application in JavaScript. The need has arisen to transfer certa ...

"Each time the header of the BS5 accordion is clicked, it van

While using the BS5 accordion, I noticed that it's behaving differently than described in the documentation. Whenever I click on the header, it disappears. <link href="//cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="style ...

Comparing Fetch and Axios: Which is Better?

Currently delving into the realms of axios and the fetch API, I am experimenting with sending requests using both methods. Here is an example of a POST request using the fetch API: let response = await fetch('https://online.yoco.com/v1/charges/&ap ...

Problem with AngularJS function execution - require a delay in executing the second function

I am developing a small utility in AngularJS to manage the shopping cart for users. Here is the code snippet from my cart-service.js file: var myStoreCartService = angular.module("myStoreCartService", []); myStoreCartService.factory('Cart', fu ...

Two consecutive instances of the outcome recorded in the ajax table

I am trying to add data to a table, but one of my results is appearing twice. Here is the JSON response I received: groupname […] 0 {…} survey_id 2 group_name DEMO 1 {…} survey_id 1 group_name TEST This is what my AJAX success function ...

What is the best way to keep track of choices made in 'mat-list-option' while using 'cdk-virtual-scroll-viewport'?

I have been working on implementing mat-list-option within cdk-virtual-scroll-viewport in an Angular 14 project. I came across a demo project in Angular 11 that helped me set up this implementation. In the Angular 11 demo, scrolling functions perfectly an ...

The sticky positioning does not function properly when used with the display property set to table-row-group in

I have a table with data that I want to keep the header sticky. The structure of my table is as follows: HTML <div class='table' id='table'> <div class='header row'> <!-- removing row class, althoug ...

Custom directive with nested objects within a scope object

What is preventing me from having a binding in a nested object within my scope object, as demonstrated here: app.directive('myDirective', function() { return { scope: { dropdown: { option: '=selectedO ...

Manipulate image position with touchmove event using CSS3 transformations on an iPad

Looking to adjust the position of an image on my iPad3, but running into some difficulties. The movement isn't as smooth as desired and seems to lag behind the gestures being made. This is what I have so far (a 3MB base64 image) <img src="data:i ...

Centralized and standardized approach to invoking a function

Explaining this concept may be a bit challenging, but I'll give it a shot: Within the .boxes class div, there are looped elements with the class .box. Each .box element contains two child elements: one with the class .box-header and another with the ...

WooCommerce - Custom button feature for canvas art and paintings. [Coding in PHP, Javascript, HTML, and CSS]

I am in need of a plugin or function that can create a pop-up modal displaying size information for paintings on WooCommerce. While there are many options available for showing size charts and tables, I have not been able to find one specifically designed ...

No matter how many times I modified the code in the ReactDOM.render() method within my index.js file, the end result remained unchanged

When I ran npx create-react-app my-app, and then proceeded to cd my-app and npm start, a browser opened on localhost:3000. While looking at the index.js file, I noticed that the ReactDOM.render() method included the following code: ReactDOM.render( <Rea ...