After selecting a subitem, the side menu will automatically close

I designed a side menu that expands to reveal submenus. However, when I click on a subitem within a submenu, the entire list closes. The HTML, CSS, and JavaScript code all appear to be correct as the template is functioning properly. But as soon as I insert a link into the href attribute and click on a subitem, the entire sublist collapses again. Essentially, every time I switch to another page, the whole menu resets, whereas I want the submenu list to remain open. I've tried looking into similar issues but haven't been able to find a solution. As a beginner in front-end development, I'm struggling with this issue. Below are snippets of the relevant code. Any help would be greatly appreciated.

$('.feat-btn').click(function() {
  $('nav ul .feat-show').toggleClass("show");
  $('nav ul .first').toggleClass("rotate");
});
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300&display=swap');
* {
  margin: 0;
  padding: 0;
  user-select: none;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

.sidebar {
  position: fixed;
  width: 400px;
  height: 100%;
  left: 0;
  background: #1b1b1b;
}

nav ul {
  background: #1b1b1b;
  height: 100%;
  width: 100%;
  list-style: none;
}

/* Rest of the CSS code... */

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav class="sidebar">
  <ul>
    <li><a class="feat-btn">First element<span class="fas fa-caret-downfirst"></span></a>
      <ul class="feat-show">
        <li><a href="some link">First subitem</a></li>
        <li><a href="some link">Second subitem</a></li>
        <li><a href="some link">Third subitem</a></li>
      </ul>
    </li>
  </ul>
</nav>

Answer №1

Here are some suggestions for changing the URL:

1. Use javascript:void(0)

<a href="javascript:void(0)" id="loginlink">login</a>

2. Disable the link handle

<a href="Some link" onClick="return false;" id="loginlink">login</a>

3. Replace the a tag with span

4. Use href="#"

<a href="#" id="loginlink">login</a>

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

The functionality of Material UI Slider components becomes less responsive when enclosed and rendered in JSX

Why is the Material UI's Slider not working smoothly when called in JSX as shown below? SliderAndValue.js import { Slider } from "@material-ui/core"; import { useState } from "react"; import "./styles.css"; export const ...

The Bootstrap4 classes for buttons, including btn and btn-primary, do not properly style links using the "a" tag

Incorporating Bootstrap version 4.1 without any custom styles on my page, I have the following code snippet for button display: <div class="btn-group" role="group" aria-label=""> <a class="btn btn-primary" type="button" href="{% url 'website ...

Fluidity of Containers on Mobile Devices

I currently have a list structured like this: https://i.sstatic.net/ei4Xt.png In order to display more details about each list item, I've added a hidden div that can be toggled open like so: https://i.sstatic.net/61k5Z.png While this works fine on ...

What is the best way to extract all values from an array that meet a specific condition?

I have an array structured like the following and I'm looking to retrieve all values where the description matches a given value. I understand how to retrieve the first array value where the condition is met, but I'm unsure of how to retrieve all ...

I need to fetch data from mongoDB by allowing the user to input a name into a search field, and then retrieve all documents that correspond to that search term

I am currently able to query the database by finding a specific key:value pair in the documents. However, I would like to enhance this functionality by allowing users to input their own search criteria as an argument in the function. Right now, I have hard ...

NG build error: Module parsing failed due to an unexpected token - no updates made

Two days ago, out of nowhere, we started encountering build errors during deployment using GitLab CI. No alterations have been made to the build scripts, and none of the versions of NPM, NG, or Angular have been modified. The same compilation commands cont ...

Using jQuery, the submenu item will receive a class based on its data id when its parent has the active class

I am struggling to add a class to the child menu when its parent has an active class. The child menu may have multiple parents. Although I wrote this code, it doesn't seem to be functioning as intended. if ($('.megamenu__list li[data-id ...

Do not use npm to install underscore libraries

How can I resolve the error I encountered while attempting to install packages using npm? Here is my packages file: "dependencies": { "express": "~3.3.6", "socket.io": "0.9.16", "jade": "~0.35.0", "less-middleware": "~0.1.12", "redis ...

Incorporating telepat-io into a Java Struts enterprise solution

Our J2EE enterprise application, built on Java Struts, has a static front end. Our team's architect has opted to enhance the user authentication by incorporating Ajax and JavaScript functionalities using telepat-io. The project is currently managed w ...

I am currently working on adjusting the first two columns of the table, but I am encountering some issues with

I have successfully fixed the first 2 columns of a table, but now there is an extra row showing up in the table header. .headcol { position:absolute; width:7em; top:auto; left: 0px; } .headcol2 { position:absolute; width:15em; top:auto ...

Solving issues with flexboxes in Safari 5.1.7 on Windows

I am currently utilizing flex boxes for managing the layouts of my website. However, it seems that the Safari version for Windows (5.1.7) is outdated and causing all flex boxes to be dysfunctional. On MacOS systems, the same version 12.x works correctly. ...

A guide on comparing a string with a ReactJS application to a collection of strings and showcasing the outcome

In one of my React Components, I have an input field that will be used to enter email IDs of users. I have a list of email IDs stored on the server. When entering text in the input field, I would like suggestions for email IDs from the existing list to app ...

How can I ensure that the HTML I retrieve with $http in Angular is displayed as actual HTML and not just plain text?

I've been struggling with this issue for quite some time. Essentially, I am using a $http.post method in Angular to send an email address and message to post.php. The post.php script then outputs text based on the result of the mail() function. Howev ...

Learn how to synchronize input field with a checkbox using v-model and computed property in Vue.js

Trying to find the best way to mirror two input fields and update them dynamically based on a checkbox value. Currently, I am using computed properties with get and set features. The issue arises when the user enters "ABC" in shipping details, unchecks the ...

The useEffect function is repeatedly making API calls within a component, despite not having any dependencies specified

As a newcomer to React.Js, I'm encountering an issue with useEffect repeatedly calling an API without any specified Dependency. Is there another approach I should consider? The relevant file is located at: /pages/dashboard/speaking/[slug].js } else i ...

Tips for Ensuring Consistent Logo Appearance Across Various Browsers

The text I added to a website appears perfectly aligned on Chrome and Explorer, but on Safari, it's positioned below the logo. How can I resolve this issue without affecting the view in other browsers? The desired layout should show as "LOGO | TEXT," ...

Issue with Material UI scrollable tabs failing to render properly in Internet Explorer

Currently, we are integrating Material UI into our tab control for our React Web UI. Everything is functioning smoothly in Chrome, but when we attempted to test it in IE, the page failed to load and presented the error below: Unhandled promise rejection ...

Learning the process of using JavaScript to extract data from a JSON file containing arrays

Currently grappling with the challenge of reading a JSON file using JavaScript. Unsure if my JSON file is in the correct format with arrays, but here is what I have. [ { "passageNumber":"2.3.1", "title":"Inside and out: A bronze ...

horizontal alignment of row content to the right in HTML

Trying to align a text and a small image horizontally to the right: <div class="row float-right"> <div class="title" >Stream Chat</div> <img src="~assets/chat.svg" /> </div> ...

What could be the reason behind the disruption in this JavaScript associative array?

I'm facing an issue with my associative array, which I have confirmed through console.log to be initially: this.RegionsChecked = {"US":true,"APAC":true,"Canada":true,"France":true,"Germany":true,"India":true,"Japan":true,"LATAM":true,"MEA":true,"UK": ...