The hamburger menu for mobile devices is not functioning properly on the website's mobile version, however it operates correctly when the screen is resized

Currently, I am facing an issue with the hamburger menu not responding on my mobile device. Oddly enough, it does work when I resize my browser window to mimic a mobile size. There seems to be a glitch happening, but I'm struggling to pinpoint the exact cause. It's important to note that due to this project being for school, I am limited in using a lot of classes or IDs.

Here is the code snippet in question:

 <nav class="navbar">
        <a href="index.html">
            <img src="images/spotify.svg" alt="Logo van spotify">
        </a>

        <!-- Use span for hamburger bars -->
        <a href="#" onclick="myFunction()" class="toggle-button">
            <span></span>
            <span></span>
            <span></span>
        </a>
        <div id="active" class="navbar-links">
            <ul>
                <li><a href="premium.html">Premium</a></li>
                <li><a href="premium.html">Help</a></li>
                <li><a href="premium.html">Downloaden</a></li>
                <li><a href="premium.html">Account</a></li>
                <li><a href="premium.html">Uitloggen</a></li>
            </ul>
        </div>
    </nav>`

JS code snippet:

`function myFunction () {
   document.getElementById("active").classList.toggle("show")
}`

What I've attempted:

I experimented with other JavaScript functions, but they mostly broke my code. One did seem to somewhat work though:

`function myFunction() {
  var x = document.getElementById("active");
  if (x.style.display === "block") {
    x.style.display = "none";
  } else {
    x.style.display = "block";
  }
}

Although this code appeared to function correctly on desktop and mobile devices, it caused my navbar links to disappear entirely. This unexpected behavior left me puzzled as to why precisely it was happening. As I am a novice in this field, any assistance would be greatly appreciated.

You can find my GitHub repository here: https://github.com/Koayz/Spotify-Bryan-van-de-Pol And my website is live at:

Answer №1

While the hamburger menu is functioning properly on Samsung Mobile Browser, it seems to be encountering issues on Chrome Mobile Browser. This indicates that there may be a CSS rule present that is not compatible across different browsers. I will further investigate your git repository to identify any potential inconsistencies.


Upon closer examination, it appears that the use of empty spans to replicate the functionality of the hamburger menu could be causing the problem. In Firefox web browser, clicking anywhere on the 'toggle-button' <a> tag activates the menu. However, in Chrome web browser, clicking directly on the spans does not trigger any action. This discrepancy suggests that only the spans are being clicked on in the mobile version.

My recommendation would be to replace the hamburger spans with an actual <svg> element (which can easily be sourced online) and associate the function with that element. Alternatively, you could attach the function to both the spans individually as well as to the container element.


If the nav elements do not have a specified position, they will naturally flow within the document structure. To address this issue, you can set the height of the navbar to 50px and then change the position of navbar-links to absolute, adding a margin-top of 50px. Remember to adjust or unset these properties for larger screen views accordingly.

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

Activating/Deactivating Checkbox using Jquery

I'm having trouble getting this code to function properly when there are multiple instances of datepicker on the page. When selecting the option "I currently work here," it is affecting all groups instead of just the current one, which is not the des ...

Showcasing an item hidden behind text and a dropdown menu

I have made changes to the background image in my Wordpress theme and now I want to display another image on top of the white content area. To achieve this, I used the following code: img style="position: absolute; top:244px; left: 220px;" src="http://ww ...

Using a JavaScript variable inside a jQuery string

Does anyone have suggestions for what I may be doing incorrectly here? VIEW DEMO HTML Code <div id="usercurrentccbox"> <div class="cardChoice"> <label for="mastercard"></label> </div> </div> JQUERY Sc ...

Implement Material-UI's built-in validation for form submission

I'm in the process of setting up a form with validation: import React from 'react'; import { useForm } from "react-hook-form"; import axios, {AxiosResponse} from "axios"; import {Box, Button, Container, Grid, Typography} ...

Troubleshooting: Vue.js file upload encountering OPTIONS 404 error

In my express app, I have configured CORS and most of the routes are working fine. However, I'm facing an issue with a specific component used for uploading images: <input type="file" class="form-control" @change="imageChanged"> <div @clic ...

The iPage server has encountered a 403 Forbidden Error, preventing

My website is linked with WordPress, but I'm encountering a 403 forbidden error in the sub-menu. Can someone assist me with this issue? I have uploaded a screenshot of the folder in iPage here Additionally, I want my WordPress page to show up correc ...

Data containing visual content is not being successfully transferred to the MySQL database

I am having an issue where the code is not sending data to the database after clicking on submit. I have also attempted using the POST method with no success. These are the table names in my database: id (AI), image, name, dob, fathername, fatheroccu ...

Manipulating contextual selectors

Have you ever tried reverting or overriding attributes from contextual selectors? .card { padding-top: 20px; ... } .card .card-header { padding: 0 20px; } .card .card-header.news { padding-top: 0px; } Do you think it's possible to elimina ...

Steps for adding a favicon to Content-Type: application/json

In my implementation using node.js, I have an API that returns a response with Content-Type: application/json. Here is an example: module.exports={ api: (req, res) => { let data = {"data": [1, 2, 3]}; res.status(200).json(d ...

Tips for automatically sending data back to the server every two seconds using ASP.NET Web Form

My Textbox looks like this : <asp:TextBox runat="server" ID="Text1"></asp:TextBox> I need my Textbox to send data back to the server every two seconds using setInterval in JavaScript. ...

Having more than one controller for a single view in AngularJS

Is it possible to use multiple controllers for a single URL view in AngularJS? I am having trouble finding documentation on this. I want to have a controller that switches the page header title on all pages, but some pages already have a controller. app.j ...

What are the proper ways to implement JavaScript conditions?

Having an issue with my slider function, as it currently only works once with the moveRight() method. To address this problem, I attempted to implement a condition that disables the move function and modifies the attributes of links on the second click. H ...

Running Javascript code after rendering a HandleBars template in Node/Express

I have a Node.js application where I am trying to load data into a jQuery datatable after the data has been fetched. Currently, I am able to populate the table with the data, but I am facing issues initializing the datatable. Here is how I render the temp ...

Stop receiving notifications for channel events in Stream with the GetStream.io Chat API

I'm having trouble understanding how to stop listening for channel events in JavaScript using the Stream Chat API. According to the documentation: // remove the handler for all "message.new" events on the channel channel.off("message.new", myChanne ...

The background-size property fails to function properly on mobile devices

It's really puzzling why this issue is happening, perhaps it's due to my fatigue. The problem lies in the fact that the "background-size" property isn't functioning on my mobile devices; however, it works perfectly when I try to simulate it ...

Creating a carousel with three thumbnails arranged in two rows

Currently, I am using a slider for my thumbnails. If you want to check out the code for this thumbnail slider, click on this link: thumbnails slider code My goal is to display 6 thumbnails in total, with 3 thumbnails shown in each row (2 rows total). Add ...

Is there a way to replicate ajaxStart and ajaxStop functions without using jQuery?

After reviewing the extensive jQuery code, I'm wondering if this task would be simple to accomplish. Any suggestions on how to approach it? I'm interested in using this not for a webpage, but for a C# application that needs to monitor ajax activ ...

Error encountered: Firebase cloud function in Node.js V10 has experienced a parsing issue due to an unexpected token 'select

Each time I try to deploy my cloud function, I encounter a Parsing error: Unexpected token selectWinner. I attempted to resolve this by updating the parser options in .eslintrc.json to utilize ecmaVersion 2017, but unfortunately, that did not solve the is ...

The click event listener declared with v-on inside a Vue Component fails to trigger

I am currently working on a Vue instance for a sidebar within my application that displays a list of menu items. In order to achieve this, I have created a local component with the following template structure: template:'<div><li class="cust ...

Modify a Google Docs script for use in Google Sheets

I created a function called "myFunk()" that works flawlessly in Google Docs. It essentially looks for fields marked with ## in a sheet and replaces them with user input. However, when I attempt to run it in Sheets after making some changes to the functions ...