Navigation menu with submenus containing buttons

I attempted to incorporate a dropdown into my existing navigation bar, but unfortunately, the dropdown content disappeared after adding the necessary code. I am now at a loss on how to troubleshoot this issue and make the dropdown function properly. Despite making several edits, the dropdown menu is visible, but clicking on any link does not yield any results. The DROPDOWN and dropbtn were included in the previous code. Can anyone provide assistance with resolving this problem? Thank you in advance!

Here is the relevant code snippet:

<!DOCTYPE html>
<html>
<!-- remaining HTML code here -->

Answer №1

If you're looking to streamline your website design, using Bootstrap is a great option. It simplifies the process and allows for added functionality directly within your HTML code.

To explore Bootstrap v4.6 navigation bars, check out this link. You can also access all the documentation here.

Here's an example of how you could integrate Bootstrap into your site:

<html>
  <head>
     <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4a6ababb0b7b0b6a5b484f0eaf2eaf5">[email protected]</a>/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="147e656171666d54273a213a25">[email protected]</a>/dist/jquery.slim.min.js" defer></script>
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f6949999828582849786b6c2d8c0d8c7">[email protected]</a>/dist/js/bootstrap.bundle.min.js" defer></script>
    <title>MENU | Introduction Menu</title>
    <meta charset=utf-8>
  </head>
  
  <body>
    <!-- Start of navbar -->
    <nav class="navbar navbar-expand-sm navbar-dark" style="background:#FFBD35">

      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNavDropdown">
        <ul class="navbar-nav">

          <!-- Section_1 link -->
          <li class="nav-item active">
            <a class="nav-link" href="#">Introduction Menu<span class="sr-only">(current)</span></a>
          </li>

          <!-- Section_2 link -->
          <li class="nav-item">
            <a class="nav-link" href="#">Online Classes</a>
          </li>

          <!-- Section_3 link -->
          <li class="nav-item">
            <a class="nav-link" href="#">Interactive Tests</a>
          </li>

           <!-- Dropdown -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-expanded="false">
               <i class="fa fa-user"></i>&nbsp; Dropdown
            </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
              <a class="dropdown-item" href="#">Action</a>
              <a class="dropdown-item" href="#">Another Action</a>
              <a class="dropdown-item" href="#">Something Else Here</a>
            </div>
          </li>

        </ul>
      </div>
    </nav>
    <!-- End of navbar -->
  
  </body>
</html>

Answer №2

It seems like you're looking for a basic dropdown menu using JavaScript, is that correct?

Here's a simple example:

HTML:

<html lang="en">
  <head>
    <!-- your header settings -->
    <link rel="stylesheet" href="page.css">
  </head>
  <body>
    <header>
      <span id="dropbtn">
        <span class="fa fa-user"></span>
        <span>Dropdown</span>
        <span class="fa fa-caret-down"></span>
      </span>
      <div id="dropdown-content" class="hidden">
        <a href="#">Link 1</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
      </div>  
    </header>

    <script src="menu.js"></script>
  </body>
</html>

In this sample, HTML, CSS, and JS are in separate files for better organization and reusability.

CSS:

.hidden {
  display:none;
}

JavaScript:

function clickListener_btn(event){
  /** @type {HTMLElement} */
  let clickedElement = event.currentTarget();

  /** @type {HTMLElement} */
  let dropDownMenu = document.querySelector('#dropdown-content');
  if(dropDownMenu.classList.has('hidden') {
    dropDownMenu.classList.remove('hidden');
  } else {
    dropDownMenu.classList.add('hidden');
  }
}

/** @type {HTMLElement} */
let btn = document.querySelector('#dropbtn');

btn.addEventListener('click', clickListener_btn);

One issue with your code is using an <a> tag as a button, which triggers a reload unless the href attribute is set. Using a <span> element as a button avoids this problem.

I hope this explanation is helpful to you. Additionally, consider searching for more dropdown menu examples to improve your code.

You could also explore creating dropdowns using only CSS, but it may be more challenging for beginners. Alternatively, look up "CSS only dropdown" on your favorite search engine for more resources.

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

NodeJs - Issue: Headers cannot be changed once they are sent & TypeError: req.next is undefined

My goal is to retrieve data from a MySQL database by calling methods to insert and read information. The reason I am doing this is because node.js operates asynchronously. Here is my code: exports.list = function(req, res){ var moduleRows; req.getCo ...

Linking query branches without encountering the "Exceeded the number of hooks rendered during the previous render" error

This apollo client utilizes a rest link to interact with 2 APIs. The first API returns the value and ID of a record, while the second API provides additional information about the same record. I combine this information to render the content without using ...

"message": "Please provide a valid user path.",

When attempting to store the user in the database, the response indicates that the "user" path is required even though the user value is present in req.body and logged for verification purposes. However, the issue persists with storing it in the database. ...

Problem with deleting or substituting symbols and character codes within a String

I am attempting to send an object called dataO from Node.js/Express/EJS to the client side. On the Node.js script side: var dataO = {"first":[20000, 14000, 12000, 15000, 18000, 19000, 22000], "second":[12000, 11000, 18000, 12000, 19000, 14000, 26000]}; var ...

Automated Submission and Online Posting of (Web Form) with JavaScript Script

Is there a way to automatically fill out the form on my webpage ( ) using a JavaScript function with specific parameters? The source code for the webpage can be found here: webpage sourcecode script enter image description here ...

Eliminate entry upon checkbox completion from task schedule

Currently working on developing my own to-do list web application. I have set everything up except for one thing. I am trying to figure out how to remove a checkbox from the array when it is checked. Can someone please assist me in achieving this using DOM ...

Display numerous lines (extracted from a MySQL database) in an HTML webpage

I am currently employing the following code to output multiple lines of text from a database $query_content= "select * from home "; $result_content= mysql_query($query_content,$con); while ($text = mysql_fetch_array($result_content)) { $content = $tex ...

Utilize jQuery to dynamically add a CSS class to a button

When using jQuery to create dynamic buttons, is there a way to add a CSS class to the button during creation without needing an extra line of JavaScript to add the class afterwards? Below is a example code snippet: $.each(data, function (index, value) { ...

What is preventing me from navigating to other pages in my React application?

Recently, I have been experimenting with ReactJS and encountered an issue where I couldn't access my other pages. The code snippet provided below seems to be the root of the problem. I am in the process of developing a multi-page application using Re ...

Why doesn't setting the SVG viewBox to "0 0 100 100" scale my path to 100%?

My current dilemma involves an SVG path that is only displaying at 50% of its intended size. How can I adjust it to show at 100%? Below is the code snippet in question: <div className="svgPathContainer"> <svg width="100%" he ...

React is throwing an error because it cannot access the property 'length' of an undefined value

TypeError: Cannot read property 'length' of undefined When I try to run my React app, I keep getting this error message from the compiler. What steps should I take to resolve this issue? request = (start,end) => { if(this.state.teams.lengt ...

Error message: "Root resolution error encountered". Need assistance in granting watchman access?

After setting up a react-native starter project, I was prompted to allow watchman access to my system's files. Unfortunately, I declined and now whenever I try to run the folder, I encounter the following error: Error: unable to resolve root /Users/ck ...

Expanding functions consist of numerous components

How can I modify this script to work on multiple elements? For example, if a user clicks on a specific link (see demo) labeled "I'd like to expand the article linked here", only that particular link should expand an element, not all like it currently ...

What is the process for adding new data to a data source without overwriting existing information?

I need assistance with a react native app I am developing. I am currently facing an issue where the data received from a fetch request needs to be displayed in a list view. However, each time new data is fetched, it overwrites the previously received data ...

What is the best way to add a background image to a div without a fixed height in order for it to span the full width and

I am trying to incorporate a background image into a div element in such a way that it behaves like a background image in the body. I have looked at an example here: http://plnkr.co/edit/gFZZgPmSKMDu3gZEp1H0?p=preview where the width and height adjust ba ...

Struggling to get collapsible feature functioning on website

I'm trying to create a collapsible DIV, and I found a solution on Stack Overflow. However, I'm having trouble getting it to work on my website. I created a fiddle with the full code, and it works there. But when I implement it on my site, the con ...

Troubleshooting auth error with Android and nativescript-plugin-firebase

I am currently utilizing this plugin in my application: https://github.com/EddyVerbruggen/nativescript-plugin-firebase Unfortunately, when using my real device on a 3G network, I encounter the following error: auth/network-request-failed Thrown if a netw ...

Mastering the art of debugging a mongoose action in node.js

I am utilizing mongoose for connecting my node.js app with mongoDB. However, I am facing an issue where the database does not get updated when I create or update a model instance. How can I effectively debug and identify what goes wrong in the create or up ...

What are the steps for configuring a dynamic variable?

One issue I encountered was setting up a variable in vue that updates when the screen orientation changes. Despite adding this variable to the data object, it did not become reactive as expected. This is my initial data function for the component: data() ...

Attempting to connect JQuery to a different page through a data attribute connection

I am currently working on a slider that connects slides from an external page through data attributes. Here is the setup: Main Page <div id="sitewrapper"> <section class="sliderwrapper"> <div id="slider" data-source="slides-pa ...