Keep the submenu visible upon clicking the sign-in form / Adjust the background color when the parent li is selected

I am facing two issues with my navigation menu that has dropdown lists:

  1. When clicking on a parent li, its submenu is displayed, but it gets hidden when clicking on another parent li or anywhere else on the page.

For the first li.parent which contains a login form, clicking on the form doesn't allow me to fill in the login details as it gets hidden. How can I interact with the login form without it hiding, but still close it when clicking elsewhere on the page?

Additionally,

2) I want the background color of the li.parent to change when one of its submenus is opened and return to its original color when the submenu is closed.

//HEADER NAV-BAR SCRIPTS:

//Show Submenus when clicking on Parent / Hide Submenus when clicking other parent/elsewhere

function hide_sub_navs() {
        $('.parent ul').hide().removeClass("shown");
      }

      $(function() {
        hide_sub_navs();
        $(".parent").click(function(event) {
          event.stopPropagation();
          var clicked = this;
          var sub_menu = $(clicked).find("ul");
          if (sub_menu.hasClass("shown")) {
            sub_menu.hide().removeClass("shown");
          } else {
            sub_menu.show().addClass("shown");
            $(".parent").each(function() {
              var next_li = this;
              if (next_li != clicked) {
                $(next_li).find("ul").hide().removeClass("shown");
              }
            });
          }
        });
        $(window).click(hide_sub_navs);
      });
/** NAV MENU **/
div#nav-bar {
  float: right;
  display: inline-block;
  height: 34px;
  width: 40%;
  clear: right;
  padding: 0px 20px;
  background-color: #FFF;
  overflow: hidden;
}

/* Rest of the CSS styles remain the same */

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>

<!-- Navigation menu HTML code here -->

 </body>

Answer №1

After some experimentation, I have come up with this code. I tested it on Boostrap Studio and it seemed to work fine for me.

I made some changes to the HTML li.parent elements as follows:

<li id="account" class="parent">Account
<li id="bookings" class="parent">
<li id="support" class="parent">Support

I assigned an id to each parent li. Additionally, each submenu now has its own id (#submenu1, #submenu2, #submenu3).

The script I implemented ensures that the submenu stays open when I click anywhere within it, and closes if I click on a different parent or anywhere else in the HTML file.

$('html, .parent').click(function() {
$('#submenu1').hide();
});

$('#mainmenu, #submenu1').click(function(event) {
event.stopPropagation();
});

$('#account').click(function(event) {
$('#submenu1').toggle();
});



$('html, .parent').click(function() {
$('#submenu2').hide();
});

$('#mainmenu, #submenu2').click(function(event) {
event.stopPropagation();
});

$('#bookings').click(function(event) {
$('#submenu2').toggle();
});



$('html, .parent').click(function() {
$('#submenu3').hide();
});

$('#mainmenu, #submenu3').click(function(event) {
event.stopPropagation();
});

$('#support').click(function(event) {
$('#submenu3').toggle();
});

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

Validating Date and Time in Angular 6 using ngIf within an HTML template

I am currently utilizing Angular6. Displayed below is the ngfor with a div. The API provides the meeting date and time along with the status, which is usually listed as upcoming. For example, if the date is 09/18/2018 and the time is 5.30 PM. <div cl ...

Ensuring Data Accuracy in Angular 2 Forms

Currently, I am working on form validation using Angular 2 and encountered an issue with the error message Cannot read property 'valid' of undefined. The HTML file I am working on contains a form structure like this: <form id="commentform" c ...

Generating elements added at various depths within an HTML document with the help of JavaScript

create_new.append("div") .append("form").merge(update_5) .attr("action", d => d.market) .attr("target","_blank") .style("width","100%") .style("height","282") .append("input").merge(update_5) .attr("type","submit") ...

Using the react-form library to create nested components with react.cloneElement

There is an issue that needs fixing with the library called react-form. Here is the error message I am currently facing: Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) ...

Is it possible to replace multiple li elements with multiple ul elements within a single ul for better organization?

In the world of HTML and CSS, there are endless possibilities for creating unique designs. Recently, I stumbled upon an interesting question that has been lingering in my mind without a clear answer. Typically, people use a single "ul" element to create a ...

Refreshing specific iframes without having to reload the entire webpage using jQuery

As the page loads initially, a hidden iframe and other visible ones are present. When a button is clicked, one of the visible iframes needs to be hidden while another becomes visible (with its src attribute set accordingly). The goal is to achieve this wit ...

Tips on utilizing Ajax for updating the RenderBody() segment

Can anyone help me understand why my Ajax.ActionLink menu item is calling JavaScript twice when I try to use it for the second time? I simply want to update the RenderBody() after clicking on a menu item. _Layout.cshtml: ... <body> <div i ...

Is there a way for me to acquire the Amazon Fire TV Series?

I'm currently working on developing a web app for Amazon Fire TV, and I require individual authorization for each app. My plan is to utilize the Amazon Fire TV serial number for this purpose. However, I am unsure how to retrieve this serial using Jav ...

Trouble with Bootstrap 5 Dropdown Functionality

Currently, I am using Bootstrap 5 in conjunction with Django to create a website and I'm encountering difficulties with getting a dropdown feature to work properly. Despite copying the code directly from w3schools, it fails to function when I load the ...

Using ngModel to retrieve and display only the month, year, and date

Currently, I am working with an interface named Person which includes fields such as name, last name, birthday, and others. However, I am facing a confusion when it comes to the person's birthday format, as it contains some additional letters at the e ...

The function HandleKeyPress is failing to recognize the input from the down arrow

I'm currently working on developing a customized and user-friendly select input using React.js. My goal is to have the functionality where the up and down arrow keys behave like the tab key within the context of selecting options. In order to achieve ...

An issue arises when attempting to utilize v-model with a file input

Is there a way to reset a file input field in Vue.js after uploading a file? I attempted to set the v-model value to null, but encountered an error message that said: File inputs are read only. Use a v-on:change listener instead. This is my current cod ...

Darkening the background of HTML and CSS text to black

I'm having trouble removing the black background from the navigation. It doesn't appear to be styled in black when I inspect each element individually. Here is an image showing the issue: https://i.stack.imgur.com/gvbn8.png @charset "UTF-8"; ...

Is there a way to manipulate the DOM without relying on a library like jQuery?

My usual go-to method for manipulating the DOM involves jQuery, like this: var mything = $("#mything"); mything.on("click", function() { mything.addClass("red"); mything.html("I have sinned."); }); Now I am looking to achieve the same result usin ...

Include two arguments when making a $http POST request

I am facing a situation where a single property can have multiple images, but each image can only be assigned to that one property (one-to-many relationship with propertyId as foreign key). In the HTML, I am fetching the propertyId and passing it along wi ...

How can I choose input from various inputs within a single form?

Context Summary Temporary SIN numbers in Canada start with '9' and are given to immigrants awaiting their official SIN. When immigrants receive their official SIN, the system needs to update the SIN based on the citizen's name and date of b ...

Are you experiencing a clash among various JS files in your WordPress installation?

I'm in a bit of a bind here. I've been tasked with working on a Wordpress website for a friend, using a free theme that he provided. Naturally, he wants some modifications made, so I've been editing the theme accordingly. The theme relies on ...

What could be the reason behind the index not getting properly set for the array that was cloned afterward?

I need assistance with a code snippet that clones an array, resets the index (0, 1, 2 ...), and stores it in a variable named buildingsPayload: console.log('1:', this.buildings) const buildingsPayload = this.buildings.map((building, index) => ...

attach an event listener for the click event

I have a button set up like this Inside index.html: <body> <section> <button id="open-file">Open File</button> ...(series of other similar buttons)... </section> </body> <script> requir ...

Tips on adjusting the CSS to fix the scrolling issue caused by images in a carousel slider

I am facing an issue where a scroll is being created and it appears offset on different screen sizes: https://i.sstatic.net/KYTCN.jpg I need help to resolve this problem and make it responsive. Even after trying to add overflow: hidden; it still doesn&a ...