Listening for events on an image does not change the visibility of an unordered list

Whenever the user clicks on the menu bar icon at the top right corner of the page, the display property of the navigation links should toggle between none and block. Two global variables are utilized in this process - one for the menu button (to detect clicks) and one for the ul element (to control its display property).

var menuButton = document.getElementById("menuicon");
var menu = document.getElementById("navlink");


function handleMenuClick() {
  if (menu.style.display == none) {
    menu.style.display = "block";
  } else {
    menu.style.display = "none";
  }
}

menuButton.addEventListener("click", handleMenuClick);
...and so on...

Answer №1

One way to achieve this is by utilizing the setAttribute() method. It's important to remember to enclose 'none' in single quotes when implementing this solution.

var menuButton = document.getElementById("menuicon");
var menu = document.getElementById("navlink");

function menuclick() {
  if (menu.style.display === 'none') {
    menu.setAttribute("style", "display:block");
  } else {
    menu.setAttribute("style", "display:none");
  }
}

menuButton.addEventListener("click", menuclick);

Answer №2

If you want to control the visibility of menu items, it's better to utilize a css class rather than inline styles. Check out the Element.classList.toggle method for reference:

var menuButton = document.getElementById("menuicon");
var menu = document.getElementById("navlink");


function toggleMenu() {
  menu.classList.toggle("active");
}

menuButton.addEventListener("click", toggleMenu);
body {
  background-color: rgb(40, 40, 40);
  font-size: 16px;
  margin: 0 auto;
  font-family: helvetica, arial;
}
header {
  position: relative;
  background: linear-gradient(rgb(40, 40, 140), rgb(50, 50, 180));
  min-height: 40px;
  box-shadow: 0px 0px 5px black inset;
}
header h1 {
  position: relative;
  padding: 10px;
  font-style: italic;
  font-size: 1.2em;
  font-weight: bold;
  color: white;
}
#h1s {
  font-size: .6em;
}
img {
  position: absolute;
  top: 0px;
  right: 10px;
}
header ul {
  position: relative;
  display: none;
}
header ul.active {
  display: block;
}
header ul li {
  background-color: red;
  text-align: center;
  height: 20px;
  background-color: grey;
  box-shadow: 0px 0px 1px black inset;
}
header ul li a {
  text-decoration: none;
  color: white;
  line-height: 20px;
  font-size: .6em;
}
<div id="page">
  <header>
    <h1> Cole Pratt <span id="h1s"> No.2968615 </span> </h1>
    <img src="images/menuicon.png" alt="menuicon" width="40" id="menuicon" />
    <ul id="navlink">
      <li><a href="#">home</a></li>
      <li><a href="#">about</a></li>
      <li><a href="#">bio</a></li>
    </ul>
  </header>
</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

Transfer PHP's uniqid() function to real-time using JavaScript with jQuery

Seeking a compact JavaScript(jQuery) code snippet to achieve this: date("r",hexdec(substr(uniqid(),0,8))); Your assistance is highly appreciated. Thank you. ...

It's not possible to add additional options to the select input in a react

Hi there! I'm currently facing an issue while trying to retrieve a list of options from the backend, map them to another list of options, and add them to a main list. Unfortunately, my attempts have not been successful. Could anyone offer some advice ...

Downloading Files from Your Cordova Application to Your Device

I am currently developing a mobile application using Cordova. In the app, there is a tutorial available in .docx format that needs to be accessed on the device. The current method involves the user clicking a button to view the tutorial document, which the ...

Incorporating post data into a Partial View

Main objective: My goal is to enable users to click on a specific day on the calendar plugin and have a popup Bootstrap modal display events scheduled for that day. Current Progress: I am currently utilizing a javascript plugin called fullCalendar. With ...

Display all elements on a webpage using Javascript without the need for scrolling

I'm looking for a way to ensure all items on a webpage load immediately, without having to scroll down multiple times before running a script to find a specific item. Is there a method to have all items load at once without the need to manually scroll ...

Using Angular routing without relying on a web server to load templates

Can templates be loaded in Angular without a web server? I came across an example here: https://groups.google.com/forum/#!topic/angular/LXzaAWqWEus but it seems to only print the template paths instead of their content. Is there a functioning example of t ...

Guide on making an active link the default setting upon the initial loading of a page with Angular 2

import {Component, Input, Output, EventEmitter, OnInit, AfterContentInit} from 'angular2/core'; import {FORM_DIRECTIVES, NgFor} from 'angular2/common'; import {MenuService} from './menu.service'; import {OrderByPipe} from &apo ...

The correlation among the event loop, libuv, and the V8 engine

Exploring the intricacies of Node.js architecture has led me to several thought-provoking questions. Is the event loop a component of libuv or v8? Does the event queue operate within the event loop? And if so, is it generated by libuv, v8 engine, or th ...

What is the best way to seamlessly transition layers from one location to another as the mouse exits and re-enters the window?

I have been working on refining a parallax effect to achieve a seamless transition between two positions based on where the mouse exits and enters the window. In the JSFiddle example provided, there is a slight 'pop' that I am looking to replace ...

Trigger a function when a CSS animation reaches its completion

I am working on a small jQuery function that needs to return its value within a subfunction. The purpose behind this is so that I can later chain this function with other jQuery functions. However, I want the next chained function to start only after the m ...

placeholder for dropdown selection in Vue.js version 2.0.0

Currently, I am in the process of developing a web application using vuejs 2.0. In order to create a straightforward select input, I employed the following code: <select v-model="age"> <option value="" disabled selected hidden>Select Age&l ...

Alter the content of a div depending on the values of three different elements

Hello everyone, I am new to the world of jQuery and javascript, and I am facing a challenge that I need some help with. Within a form, there are two fields that I would like to perform calculations on. Here is what I have attempted so far: jQuery.fn.calcu ...

Tips for preventing automatic zoom on mobile devices

Check out this link for the test: The current layout of the site is exactly how I want it to be presented. However, I am facing an issue where if I set the viewport meta tag to <meta name="viewport" content="width=device-width, initial-scale=1"> i ...

Ways to merge two Vue.js components

Looking to integrate a button component with an HTML component. The goal is to receive the response from the button within the HTML component, specifically when utilizing a v-if condition. When the button is clicked, it should trigger an action in the HTML ...

JavaScript code to toggle the navigation bar on mobile devices

I am experiencing an issue with a script that is not performing the desired task. I am looking for a script that can add the Class "active" to a ul element with the id "btnMob0". Currently, my script looks like this: <script type="text/javascript"> ...

Need the equivalent syntax of Import in Nodejs

Currently, I have a Node.js application that is still utilizing CommonJS. Everything has been working smoothly so far, but I recently encountered a module that I am not sure how to import. Instead of completely restructuring my application to adhere to the ...

Top method for showcasing animated images (HTML/CSS/JS)

For my website, I want to create an engaging animation showing a coin being flipped multiple times in the air before landing on a specific side. After the animation finishes, I would like it to transform into a static image of the final result. I've ...

Clearing LocalStorage and Cache upon initial loading in Next.js

Our project requires continuous updates, and currently users are required to manually clear their cache and localStorage data after each update in order to access the new features. Is there a solution to automatically clear all cached data upon user visi ...

Animated SVG Arrow Design

I created a dynamic SVG animation that grows as you hover over it. Since I'm still learning about SVG animations, I encountered some issues with my implementation. The animation is quite straightforward - when hovering over the SVG arrow, the line sho ...

Following the recent update of Google Chrome, Sencha Touch experiences spinning issues

Since updating my Google Chrome to the latest version (v29.0.1547.57 m), I've noticed some issues with parts of my Sencha Touch app spinning. The Sencha Touch version I am using is v2.2.1. For example, when using Ext.Msg.alert, I can see the title bu ...