jQuery menu fails to toggle the class name

The toggle functionality in the Menu is not working properly. Upon clicking the toggle button, I encountered the following JavaScript error:

  "message": "Uncaught TypeError: Cannot read property 'toggle' of undefined",
  "filename": "https://stacksnippets.net/js",
  "lineno": 159,
  "colno": 44
}

//java.js
function toggleMenu() {
  document.getElementById('Menu').classlist.toggle('active');
}
* {
  margin: 0px;
  padding: 0px;
  font-family: sans-serif;
}

#Menu {
  padding: 0px;
  position: fixed;
  width: 400px;
  height: 100%;
  background: #00ff00;
  left: -400px;
}

#Menu.active {
  display: 0px;
}

#menu ul li {
  color: rgba(230, 230, 230, 0.9);
  list-style: none;
  padding: 15px 10px;
  top: 40px;
  position: relative;
  width: 200px;
  vertical-align: middle;
  cursor: pointer;
  background-color: #00ff00;
  border-top: 4px solid #ff0000;
  -webkit-transition: all 0.3s;
  -o-transition: all 0.3s;
  transition: all 0.3s;
}

#Menu .toggle-btn {
  position: absolute;
  left: 410px;
  top: 65px;
}

#Menu .toggle-btn span {
  display: block;
  width: 30px;
  height: 5px;
  background: #000;
  margin: 5px 0px;
}

#menu ul {
  list-style: none;
  margin: 0px;
  padding: 0;
}

#menu ul li:hover {
  background-color: #000;
}

#menu>ul>li {
  border-right: 4px solid #ff0000;
  border-left: 2px solid #000;
}

.bottom {
  border-bottom: 4px solid #ff0000;
}

#menu ul ul {
  transition: all 0.3s;
  opacity: 0;
  position: absolute;
  border-left: 4px solid #ff0000;
  border-bottom: 4px solid #ff0000;
  border-right: 4px solid #ff0000;
  visibility: hidden;
  left: 100%;
  top: -2%;
}

#menu ul li:hover>ul {
  opacity: 1;
  visibility: visible;
}

#Menu ul li a {
  text-decoration: none;
}

i {
  margin-left: 15px;
}

#menu>ul>li:nth-of-type(3)::after {
  content: "+";
  position: absolute;
  margin-left: 60%;
  color: #fff;
  font-size: 18px;
}

#menu>ul>li:nth-of-type(2)::after {
  content: "+";
  position: absolute;
  margin-left: 56%;
  color: #fff;
  font-size: 18px;
}
<!doctype html>
<html>

<head>
  <title>vertical menu with css</title>
  <link rel="stylesheet" type="text/css" href="style.css">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  <script src="java.js"></script>
</head>

<body>
  <div id="Menu">
    <div class="toggle-btn" onclick="toggleMenu()">
      <span></span>
      <span></span>
      <span></span>
    </div>
    <ul>
      <li><a href="#"><i class="fas fa-home">Home</i></a></li>
      <li><a href="#"><i class="fas fa-user-tie">Bjorn</i></a>
        <ul>
          <li><a href="#">Child 1 </i></a></li>
          <li><a href="#">Child 2 </i> </a></li>
          <li><a href="#">Child 3 </i></a></li>
        </ul>
        <li><a href="#"><i class="fas fa-user">Cille</i></a>
          <ul>
            <li><a href="#">Child 1 </a></li>
            <li><a href="#">Child 2 </a></li>
            <li><a href="#">Child 3 </a></li>
          </ul>
          <li><a href="#"><i class="fas fa-user-tie">David</i></a></li>
          <li class="bottom"><a href="#"><i class="fas fa-user-tie">Fin</i>/a></li>

      </ul>
    </div>
  </body>

</html>

Answer №1

There were numerous bugs present. I suggest comparing my version with yours to identify the discrepancies.

  1. Be mindful of case sensitivity (e.g. classList and classlist, #Menu and #menu)

  2. Ensure proper closure of HTML tags. Some errors were noted in this area.

  3. Remove unnecessary elements such as </i>. These may be remnants from previous copy-pasting.

Nevertheless, it is recommended to utilize a syntax-highlighting tool like an IDE. Free versions include VS Code, Atom, etc. Familiarize yourself with the syntax by referring to resources like the Mozilla Developer Network, which is a valuable knowledge base maintained by the community.

function toggleMenu() {
  document.getElementById('Menu').classList.toggle('active');
}
* {
  margin: 0px;
  padding: 0px;
  font-family: sans-serif;
}

#Menu {
  padding: 0px;
  position: fixed;
  width: 400px;
  height: 100%;
  background: #00ff00;
  left: -400px;
  transition: left 360ms ease-in;
}

#Menu.active {
  left: 0;
}

#Menu ul li {
  color: rgba(230, 230, 230, 0.9);
  list-style: none;
  padding: 15px 10px;
  top: 40px;
  position: relative;
  width: 200px;
  vertical-align: middle;
  cursor: pointer;
  background-color: #00ff00;
  border-top: 4px solid #ff0000;
  -webkit-transition: all 0.3s;
  -o-transition: all 0.3s;
  transition: all 0.3s;
}

#Menu .toggle-btn {
  position: absolute;
  left: 410px;
  top: 65px;
}

#Menu .toggle-btn span {
  display: block;
  width: 30px;
  height: 5px;
  background: #000;
  margin: 5px 0px;
}

#Menu ul {
  list-style: none;
  margin: 0px;
  padding: 0;
}

#Menu ul li:hover {
  background-color: #000;
}

#Menu>ul>li {
  border-right: 4px solid #ff0000;
  border-left: 2px solid #000;
}

.bottom {
  border-bottom: 4px solid #ff0000;
}

#Menu ul ul {
  transition: all 0.3s;
  opacity: 0;
  position: absolute;
  border-left: 4px solid #ff0000;
  border-bottom: 4px solid #ff0000;
  border-right: 4px solid #ff0000;
  visibility: hidden;
  left: 100%;
  top: -2%;
}

#Menu ul li:hover>ul {
  opacity: 1;
  visibility: visible;
}

#Menu ul li a {
  text-decoration: none;
}

i {
  margin-left: 15px;
}

#Menu>ul>li:nth-of-type(3)::after {
  content: "+";
  position: absolute;
  margin-left: 60%;
  color: #fff;
  font-size: 18px;
}

#Menu>ul>li:nth-of-type(2)::after {
  content: "+";
  position: absolute;
  margin-left: 56%;
  color: #fff;
  font-size: 18px;
}
<!doctype html>
<html>

<head>
  <title>vertical menu with css</title>
  <link rel="stylesheet" type="text/css" href="style.css">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  <script src="java.js"></script>
</head>

<body>
  <div id="Menu">
    <div class="toggle-btn" onclick="toggleMenu()">
      <span></span>
      <span></span>
      <span></span>
    </div>
    <ul>
      <li><a href="#"><i class="fas fa-home">Home</i></a></li>
      <li><a href="#"><i class="fas fa-user-tie">John</i></a>
        <ul>
          <li><a href="#">Child 1</a></li>
          <li><a href="#">Child 2</a></li>
          <li><a href="#">Child 3</a></li>
        </ul>
        <li><a href="#"><i class="fas fa-user">Jane</i></a>
          <ul>
            <li><a href="#">Child 1</a></li>
            <li><a href="#">Child 2</a></li>
            <li><a href="#">Child 3</a></li>
          </ul>
          <li><a href="#"><i class="fas fa-user-tie">David</i></a></li>
          <li class="bottom"><a href="#"><i class="fas fa-user-tie">Finch</i></a></li>

          </ul>
        </div>
      </body>

    </html>

Answer №2

It appears the issue lies in the incorrect usage of classList, which should be camel-cased as shown below:

function toggleMenu() {
  document.getElementById('Menu').classList.toggle('active');
}

Additionally, consider naming your file based on its functionality rather than using a name like java.js. This practice will help you better organize and identify features within your codebase.

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 AJAX success callback function failed to execute in cases where the dataType was set to JSONP during Cross domain Access

type = 'math'; var ajurl = "sample.com&callback=myhandler"; var datas = "cateid=" + cateid + "&type=" + type + "&pno=" + pno + "&whos=" + whos; $.ajax({ type: "GET", url: ajurl, data: datas, contentType: "application/json; ...

Facing an issue where WordPress AJAX is not showing the expected content

Currently, I am in the process of creating a WordPress website that will feature an initial display of two rows of images. Upon clicking a button, two more rows should dynamically appear. There are four key files involved in this project: case-study-ca ...

Trouble with displaying days on the Datepicker

I'm currently facing an issue with my datepicker. I have a specific set of days that should be highlighted on the calendar, and it needs to be constantly updated. For example, past days are removed from the calendar automatically to keep it current. H ...

Store the checkbox's data in the database for safekeeping

Hey there, I'm working on saving the value of a checkbox using PHP. The twist is that the value is generated through JavaScript. How can I handle this scenario and save the value using PHP? Checkbox: <input type='checkbox' name='ca ...

Tips on obtaining the element that was used as the event selector

I am facing a challenge with a specific issue. Within a div containing various elements, I have implemented a mouseover event. The issue arises when trying to target this particular div in the mouseover function, as there are multiple automatically genera ...

How can I create two buttons on one line in a Struts2 form?

My form has two buttons - one for registering and the other for canceling the form. I created them using the following code: <s:submit name="cancel" key="project.button.cancel" /> <s:submit name="login" key="form.register.registerBtn" /> How ...

Modifying the content within a DIV element

I want to make changes to my DIV. <div id="main"> <div id="one"> <div class="red"> ... </div> <img class="avatar" src="img/avatar1.jpg"/> <span class="name"> John < ...

Unable to reference the namespace 'ThemeDefinition' as a valid type within Vuetify

Looking to develop a unique theme for Vuetify v3.0.0-alpha.10 and I'm working with my vuetify.ts plugin file. import "@mdi/font/css/materialdesignicons.css"; import "vuetify/lib/styles/main.sass"; import { createVuetify, ThemeDefinition } from "v ...

Message displayed within Ng-repeat loop

Having trouble implementing a tooltip within an ng-repeat for each item in a td element. Whenever the mouse hovers over an item inside the td, I want a tooltip to display with more details. The code below shows my setup, using ng-if to prevent displaying e ...

JSON format for date and time conversion

Dear Jason, [Serializable] public class MyModel { public int Id {get;set;} public DateTime date {get;set;} } [HttpPost] public ActionResult SchedulesDropdownIndexChanged(MyModel schedule) { objScheduleModel = new ScheduleModel( ...

How to output a string in jQuery if it includes a certain character?

I need to extract all strings that include a specific word within them, like this: <div> <div> <span> <h3>In Paris (Mark Bartels) worked for about 50 years.</h3> </span> </div> ...

Enhance your user interface with a personalized icon on the JQuery date

Is it possible to use a custom icon, added by markup, to trigger the datepicker's textbox instead of the default calendar icon that shows up after applying the plugin? Thank you. ...

What is the best way to implement collision detection using raycasting?

For my university project, I am looking to implement collision similar to what is shown in this example. However, I am facing an issue where collision is only working on the top of the object. I referred to this for guidance. My goal is to add collision to ...

Guide on invoking a node.js function from an express-rendered ejs page

My express server currently has a button that triggers a POST request to activate a function in my node.js server. Instead of using a traditional POST request, I am interested in implementing something like AJAX so that the page doesn't reload. Is th ...

Using regular expressions to identify the presence of "&", parentheses, and consecutive letters in a string

I specialize in working with JavaScript and I have a need to verify if my text contains certain characters. Specifically, I want to check for the presence of parentheses (), ampersands (&), and repeating letters within the text. Here are some examples: te ...

React - Error: you have a syntax problem because there is an unexpected token <

I am working on a project using Express, pg, and react. However, I have encountered some issues with React. Here is the directory of my project index.js var express = require('express'); var server = express(); var path = require('path&ap ...

What could be causing the Babel installation to fail within Electron? Is Babel necessary for my project or can it be avoided?

Having trouble using the npm package https://www.npmjs.com/package/swipe-detect and encountering the following error message: export default function(target, callback, threshold=150) { ^^^^^^ SyntaxError: Unexpected token export at Module._compile (i ...

The "Product URL" in Woocommerce is overlapping with the "Add to Cart" button

Hey, I've encountered a strange issue. I just realized that when I go to my store, the "add to cart" button is overlapping with the product info URL. To better illustrate this problem, please see the images below: https://i.sstatic.net/mkQ8h.png Whe ...

Is it possible to share a Vue.js component by "transferring" it rather than duplicating it?

In my comment system project using Vue.js, I have over 300 comments to manage. Each comment has an admin section that appears when the cursor hovers over it. Duplicating the admin section component for each comment is not ideal as it creates unnecessary l ...

javascript is failing to display the appropriate message at the correct moment

Wrote a code to handle the type of message I should get, looping over all the rows in the table to find matching conditions. These are my conditions: - If all rows have data-mode="success" and data-pure="yes", it's correct and successful. - If any row ...