The dropdown menu seems to be missing from the navigation bar

My dropdown menu is not behaving as expected after clicking. It should be a simple dropdown menu with the tab "about," similar to the one on w3schools.

https://i.sstatic.net/VZBGZ.png

Below is the code snippet:

/* Function to toggle between hiding and showing dropdown content */
function myFunctionlang() {
  document.getElementById("myDropdown").classList.toggle("showlang");
}

// Close the dropdown if user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtnlang')) {
    var dropdowns = document.getElementsByClassName("dropdownlang-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('showlang')) {
        openDropdown.classList.remove('showlang');
      }
    }
  }
}
/* Styles for dropdown menu */

.dropbtnlang {
  background-color: #3498DB;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtnlang:hover,
.dropbtnlang:focus {
  background-color: #2980B9;
}

.dropdownlang {
  position: relative;
  display: inline-block;
}

.dropdownlang-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdownlang-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdownlang a:hover {
  background-color: #ddd;
}

.showlang {
  display: block;
}


/* End of dropdown styles */
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />

<a href="#"><i class="fa fa-fw fa-language" onclick="myFunctionlang()" class="dropbtnlang"></i></a>
<div id="myDropdown" class="dropdownlang-content">
  <a href="#about">Test</a>
</div>

Answer №1

It appears that the issue lies within this snippet of code:

<i class="fa fa-fw fa-language" onclick="myFunctionlang()" class="dropbtnlang"></i>
. You have mistakenly set the class attribute twice, causing a minor error in your code. I have corrected this issue, and now everything seems to be functioning properly.

/* When the user clicks on the button,
      toggle between hiding and showing the dropdown content */
function myFunctionlang() {
  document.getElementById("myDropdown").classList.toggle("showlang");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtnlang')) {
    var dropdowns = document.getElementsByClassName("dropdownlang-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('showlang')) {
        openDropdown.classList.remove('showlang');
      }
    }
  }
}
/*dropdown lang start*/

.dropbtnlang {
  background-color: #3498DB;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtnlang:hover,
.dropbtnlang:focus {
  background-color: #2980B9;
}

.dropdownlang {
  position: relative;
  display: inline-block;
}

.dropdownlang-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdownlang-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdownlang a:hover {
  background-color: #ddd;
}

.showlang {
  display: block;
}


/*dropdown lang end*/
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />

<a href="#"><i class="fa fa-fw fa-language dropbtnlang" onclick="myFunctionlang()" ></i></a>
<div id="myDropdown" class="dropdownlang-content">
  <a href="#about">Test</a>
</div>

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

Is it better to apply the font-family to the body or to the html element?

Is it more appropriate to specify the font-family CSS property on the html element or the body element? html { font-family: sans-serif; } or body { font-family: sans-serif; } I've looked into this issue online, but there doesn't seem to ...

Morris.js tutorial: Enhancing bar charts with data labels

I have this: https://i.sstatic.net/GXjur.png But I want this instead: https://i.sstatic.net/spcS2.png Does morris.js support this feature? If not, what would be the most effective method to implement it? ...

What is the process for duplicating a set of elements within an svg file and displaying the duplicate at a specific location?

SVG <svg width="200" height="200"> <g id="group"> <rect x="10" y="10" width="50" height="20" fill="teal"></rect> <circle cx=" ...

A guide on initiating a get request with URL in React

Utilizing React for the frontend and express for the backend has been my approach. By defining get methods in my express like this:- app.get('/addBill',(req,res)=>{ // --first method res.render("addBill"); }); app.get(&a ...

Error message stating 'compression is not defined' encountered while attempting to deploy a Node.js application on Heroku

Why is Heroku indicating that compression is undefined? Strangely, when I manually set process.env.NODE_ENV = 'production' and run the app with node server, everything works perfectly... Error log can be found here: https://gist.github.com/anony ...

Html content overlapping div rather than being stacked vertically

My goal is to arrange a group of divs inside another div in a vertical stack. However, I am facing an issue where the text from the last div is overlapping with the second div instead of following a proper vertical alignment where the text in the third div ...

You can click on the link within the Leaflet Popup to trigger JavaScript functionality

My leaflet map is functioning with GeoJSON polygons and popups attached to each one. These popups display information about the polygon. I want a link inside the popup that, when clicked, triggers a JavaScript function to retrieve and display smaller polyg ...

Unable to utilize JQuery function for selecting dynamically generated textbox

In the table's body, I am dynamically passing a table from a JavaScript file. For a column, I have returned a textbox as shown below: function (oObj) { return '<div id=\"uniform-chkPerOverage\"> <span ><input t ...

Can one manipulate SVG programmatically?

Looking to develop a unique conveyor belt animation that shifts items on the conveyer as you scroll down, then reverses when scrolling up. I discovered an example that's close to what I need, but instead of moving automatically, it should be triggered ...

Having difficulty running assessments on the connected components

I have encountered an issue while using the MERN stack and Redux. Specifically, I am facing errors when trying to write test cases for certain components. The error message I receive states: "Could not find "store" in the context of "C ...

Step-by-step guide on integrating the Tawk chat widget script into a Next.js React application

I am currently working on integrating a chat widget from Tawk into a next.js react app. In my _app.js file, I have added the script import tag and attempted to set up the widget like this: import Script from 'next/script' {/* <!--Start ...

Accessing data in form rules with Vuetify: tips and tricks

Is it possible to access a data element within a rule? Click here to see my code in action I'm attempting to change the value of a data element on a text field rule within a Vuetify form. While the rule itself is functioning properly, I'm enco ...

the dropdown menu toggle is not working as expected

My dropdown icon is not appearing and the menu options are not functioning properly. Despite trying to use a script to display the text when an option is clicked, it doesn't seem to be working. It appears that the toggle functionality is not working ...

Three.js Ellipse Curve Rotation

Purpose My goal is to create an EllipseCurve () where a camera will move. The Approach I Took to Achieve the Goal Below is the code snippet for creating the ellipse: var curve = new THREE.EllipseCurve( 0, 0, 1, 1, 0, 2 * Math.PI, false, ...

Vuetify autocomplete with server-side functionality

I am looking to implement infinite pagination on the Vuetify autocomplete feature. My goal is to trigger the loading of a new page of items from the backend when I scroll to the end of the menu. Initially, I attempted using the v-intersect directive as fo ...

Redux: Double rendering issue in mapStateToProps

I've recently delved into learning Redux, and I've encountered an issue that's been on my mind. import React, { useEffect } from "react"; import { connect, useDispatch } from "react-redux"; import Modal from "../Moda ...

Dealing with problems in col-md display on tablet devices

When viewing on Full Screen and mobile, the ranges panel (class="col-md-3") displays correctly. However, on tablet screens, the left column is obscured by the youtube image panel (class="col-12 col-md-9"). I have attempted various adjustments to the div s ...

Error encountered during JSON parsing: unexpected non-whitespace character found after the JSON data on line 1

I have implemented a simple JavaScript code (using AngularJS, but that's not important) like this: app = angular.module('app', []); app.controller("NavCtrl",function($scope,$http){ var serviceBase = 'api/'; $http.get(serv ...

Steps to invoke controller through ajax and transmit $_POST variables

I am trying to make an AJAX call to a controller in order to save data into a database. Once the data is saved, I want to display it in a dropdown list. However, I am facing some issues and not sure what to do next. Below is the code snippet from my view ...

What accounts for the different behavior of line-height when using units vs percentage or em in this instance?

I'm puzzled by the behavior of the CSS code provided, which is also demonstrated in this fiddle. <style type="text/css"> p { font-size: 14px; } .percentage { line-height: 150%; } .em-and-a-half { line-height: 1.5em; } .decimal { ...