Conceal sub menu when another parent menu is selected

I recently worked on a project creating a menu with submenus for a custom parallax website. When clicking on a parent menu item, the submenus remain displayed as intended. However, I now need to hide the submenus when other parent menu items are clicked. Since I am new to jQuery, I would appreciate some assistance. Below is a snippet of my code...

HTML

<ul class="navigation">
    <li data-slide="2">estate
        <ul class="navigation2">
            <li data-slide="2">land</li>
            <li data-slide="3">varietal</li>
            <li data-slide="4">people</li>
            <li data-slide="6">practices</li>
            <li data-slide="9">future offerings</li>
        </ul>
    </li>
    <li data-slide="10">about</li>
    <li data-slide="13">location</li>
    <li data-slide="14">contact</li>
</ul>

CSS

.navigation {
    position:fixed;
    text-align:center;
}
.navigation li {
    display:block;
}
.navigation li:hover, .active {
    cursor:pointer;
    text-decoration:underline;
}
.navigation2 {
    position:fixed;
    text-align:right;
    left:50%;
}
.navigation2 li {
    color:#000;
}
.navigation2 li:hover, .active {
    cursor:pointer;
    text-decoration:underline;
}

JQUERY

$('.navigation2').hide();
$('.navigation').children().click(function () {
    $(this).siblings().find('.navigation2').slideUp(1000);
    $(this).children('.navigation2').slideToggle(1000);
}).children('.navigation2').click(function (event) {
    event.stopPropagation();
}); 

Answer №1

To start off your click function, conceal the navigation2 elements of the current li's siblings.

$('.navigation').children().click(function () {
    $(this).siblings().children('.navigation2').hide(); //include this statement
    // add the rest of your code here.
});

Answer №2

Add a specific class, such as 'submenu', to your sub menus

Afterwards, when you click on any of the main menus, you can easily hide all of the submenus with this simple line of code:

$('.submenu').hide();

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

Explore beyond the limitations of an Iframe

I have embedded links in an iframe that lead to external websites, and I'm looking for a way to make them open in the parent window instead of within the iframe. Any suggestions would be greatly appreciated. Thanks! ...

Dynamic CSS Locking Table Headers - Adjustable Width and Height Featuring Both Vertical and Horizontal Scroll Bars

Is it possible to use Pure CSS to lock headers in a thead element while keeping the entire table variable width and allowing for vertical and horizontal scrolling of the content? I came across this solution, but it fixes the width of the columns and the t ...

Regular expressions can be used to locate a specific string and remove the href attribute associated with it

I need to eliminate the anchor tag associated with "check out the slideshow". The text for checking out the slideshow may change, so we must use #show-gallery to identify it from the href. var jStr = $('.gallery-wrapper').html(); cStr = jStr ...

Adding identifiers to columns in DataTables depending on the value of another attribute

I am facing a challenge with inserting the <span> tag into the "salesStatusName" column cell that already contains some data. This should only happen when the value of the last column, "completelyDelivered", is true. However, I do not want to display ...

Achieve button highlighting by hovering over the card - Bootstrap 5

Is there a way to make a button focus when hovering over a card in Bootstrap from any place on the card? Currently, the button only focuses when directly hovered over. I want to achieve the effect shown in the second image after hovering over any part of t ...

When attempting to submit form data using AJAX, the process fails. However, the form successfully submits data through $_GET method when I

Hello everyone, I'm new to this forum and this is my first post. Please excuse any mistakes in my English. I've been trying to submit form data using the codes below to a page called index.php in my signup/submit directory. Below is a snippet o ...

How can you modify the color of a FontAwesome icon?

I am currently utilizing BigCommerce and my code looks like this: .icon-cart { color: #4ca3c0 !important; } .icon-cart a{ color: #4ca3c0 !important; } Despite the above, it seems that the icon's color remains unchanged. Any suggestions or i ...

Tips for altering the distance between dots on a line

.ccw--steps { margin: 10px auto; position: relative; max-width: 500px; } .ccw--step-dot { display: inline-block; width: 15px; border-radius: 50%; height: 15px; background: blue; border: 5px solid #e8e8e8; position: relative; top: -8p ...

Refresh table with data submitted via form on current page

Here is the coding found on my homepage: <html> <head> <title>Testing PHP</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="formDiv"> <form action="t ...

How can I achieve a full-width div without using the container-fluid class while still maintaining the width and structure of

I am looking for a full div without the container-fluid class while keeping the container width fixed. The structure of the container should not change as I need another div inside the container. The structure should remain as it is. .main { padding: ...

Removing an item from a table row cannot be completed due to the inability to retrieve the list_body ID necessary for deletion

I have been working on enhancing the delete button function in my table. The id linked to the list_body must be incorporated into the delete function code. I utilized some jquery methods to render the list onto the webpage. //retrieve user list information ...

showcase every value upon submission in the form with options to edit and delete

Is it possible to display all values submitted in a form with edit and delete buttons? Currently, only one value is being displayed at a time. Whenever a new value is displayed, it replaces the old one. How can this be fixed? You can fin ...

Unable to display favicon when using the include function

How can I add the favicon link to my website? In my header.php, I have added the link for my ico favicon, but when I try to include it in my index file, the favicon does not show up. I attempted to insert the favicon code directly into the index.php file, ...

The parameters in the URL are not displayed when making an AJAX GET request

Can anyone assist me with a jQuery AJAX function issue I'm encountering when trying to make a GET request on a form? The problem lies in the fact that the parameters do not show up in the URL. Here is the code snippet: <form id="search_form" metho ...

What steps do I take to include alternative text for images I insert within the content?

Ensuring all my images have alt text is important, but I overlooked this when I added my background images to the CSS under the body tag. The following code shows the background image in my CSS that needs alt text: body { background: url('contact ...

The color type input is not responding to the onChange event

I have been searching a lot and exploring answers on Stack Overflow, yet I have had no luck. I am trying to retrieve the color value when the color is changed in an input field. Here is the code I am using, could you please assist me in finding out why it ...

Animate the menu as you scroll

I am attempting to create a jQuery top menu that adjusts its height based on the window scroll position. Using $(selector).css("height", "value") works fine, but when I try to animate it, it doesn't behave correctly. Here is my code. Thank you for you ...

Tips for generating a unique identification for every blog post

Trying to use jQuery's "prependTo" function to move the post image from one div element to another. Check out the fiddle for more details. $('.post img').prependTo('.move'); Since a class selector is used, each image will be move ...

Pop-up confirmation dialog in JQuery following an AJAX request

In order to validate on the server side whether a person with a specific registration number already exists in the database, I have implemented a process. If the person is found registered, the program flow continues as usual. However, if the number is not ...

Aligning input and submit fields perfectly in the same line

Struggling to get an input form and submit button to align perfectly in Firefox and Safari. Any advice? Here's the current CSS for the 'Keep in touch' widget on www.landedhouses.co.uk: form.mc4wp-form p{margin-left:0px !important; display: ...