Attempting to toggle the menu with jQuery does not yield any results

I am fairly new to the world of javascript and jQuery and I'm encountering some issues that are proving difficult to troubleshoot. I am experimenting with the .toggle function on a menu list, but when I click the temporary button I've set up for toggling, nothing seems to be happening.

Here is my code:

$(document).ready(function() {
  $("#menuIcon").click(function() {
    $("#menu").fadeIn(1000);
  });
});
#menuIcon {
  display: none;
}
@media screen and (max-width: 1000px) {
  #menu {
    display: none;
  }
  #menuIcon {
    display: block;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="menuIcon">Click Me</button>
<div id="menu">
  <nav>
    <ul>
      <li>Home</li>
      <li>About</li>
      <li>Page</li>
      <li>Contact</li>
    </ul>
  </nav>
</div>

Answer №1

Include the jQuery script in your code and utilize jQuery toggle to switch the display of your menu.

$("#menu").toggle("display")

At the moment, you are only fading in the menu.

See the following revised code:

<!DOCTYPE html>
<html lang="en">
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <style>
    #menuIcon{
        display: none;
    }
        @media screen and (max-width: 1000px){
            #menu{

                display: none;
            }
            #menuIcon{
                display: block;
            }
        }
    </style>
    </head>
    <body>
    <button id="menuIcon">Click Me</button>
    <div id="menu">
        <nav>
            <ul>
                <li>Home</li>
                <li>About</li>
                <li>Page</li>
                <li>Contact</li>
            </ul>
        </nav>
    </div>
    <script>
        $(document).ready(function(){
            $("#menuIcon").click(function(){
                $("#menu").toggle("display");
            })
        });
    </script>
</body>
</html>

Answer №2

To get it functioning properly, simply include the jquery cdn script.

 <!DOCTYPE html>
<html lang="en">
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <style>
    #menuIcon{
        display: none;
    }
        @media screen and (max-width: 1000px){
            #menu{

                display: none;
            }
            #menuIcon{
                display: block;
            }
        }
    </style>
    </head>
    <body>
    <button id="menuIcon">Click Me</button>
    <div id="menu">
        <nav>
            <ul>
                <li>Home</li>
                <li>About</li>
                <li>Page</li>
                <li>Contact</li>
            </ul>
        </nav>
    </div>
    <script>
        $(document).ready(function(){
            $("#menuIcon").click(function(){
                $("#menu").fadeIn(1000);
            })
        });
    </script>
</body>
</html>

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

What is the process for displaying objects within an object using HTML?

I'm developing an app using Angular and Node.js. I have a complex data structure where one object (order) contains a list of objects (items), which in turn include another list of objects (product IDs). My goal is to properly display all this data wit ...

Get the value of an HTML element

Is there a way to retrieve the value of an HTML element using PHP or JavaScript, especially when the value is dynamically loaded from another source? I have tried using jQuery with the DOM ready event, but often encounter the issue where the element's ...

Managing input jQuery with special characters such as 'ä', 'ö', 'ü' poses a unique challenge

Hey there, I'm running into a bit of trouble with my code and I can't figure out why it's not working. Here's a brief overview: I created my own auto-suggest feature similar to jQuery UI autosuggest. Unfortunately, I'm unable t ...

Verification symbols in Unicode

I'm on the hunt for a universal checkmark symbol that is compatible across various operating systems and web browsers. I've tested out the following two options: Version 1: ✓ Version 2: ✔ (source) However, these symbols seem to be working ...

Creating a semi-circle donut chart with Highcharts using the Highcharts-ng library

I have been working on integrating a Highcharts Semi-circle donut chart into my angular application using the Highcharts-ng directive. However, I seem to be facing an issue where the plotOptions section is being skipped entirely, leading to a full circle p ...

Why does it appear that Angular is undefined in this straightforward Angular demonstration?

I'm completely new to AngularJS and I've encountered an issue. Yesterday, I ran a very simple AngularJS application that I downloaded from a tutorial and it worked perfectly. The application consists of 2 files: 1) index.htm: <!DOCTYPE htm ...

Exploring the fundamentals of Jquery syntax and the powerful world of

I am facing an issue while trying to attach an onclick event during HTML generation. I am attempting to utilize the object ID for event binding using the foreach method. I am unsure about the syntax error that is causing trouble, and would appreciate some ...

Discover the index of the row when the value in the dropdown list is updated

I'm faced with a challenge regarding an HTML Table that contains a dropdown list in every row. I would like the background of each row to change whenever the value in the dropdown list is modified. Below is the code snippet: <table id="table1"> ...

Input field for postal code containing only numbers (maximum 5 digits) in Angular version 4/5

I am struggling with creating an input field that accepts numbers. If I use type="text", I can only type 5 characters of alphanumeric data, but if I use type="number", it allows any number input without limiting it to just 5 numbers. Thank you in advance f ...

Book Roulette: Your Next Random Read

I created a code for a random quote generator and now I want to create something similar, but with images this time. I am looking to add a feature on my page where it can recommend a book by displaying its cover image when a button is clicked. For the pre ...

After executing grunt serve, Bower issues a warning for jquery stating "not injected"

Recently, I had to clone a project and rebuild bower packages. It seems that jQuery has been updated, causing a warning to appear: Warning: Please check the "app/bower_components/jquery" folder for the necessary file and manually include it in your ...

Button group malfunctions on smaller screens

After integrating a button group into my new website, I noticed that the first two buttons stop functioning properly on smaller screens. Surprisingly, if I remove the text-center div, all buttons cease to work entirely. Despite attempting various solution ...

Get rid of the margins on your Wordpress theme

Currently, my Wordpress site is using the Tesseract theme which can be found at (http:// instantiwebs . com) I am interested in removing the left/right margins on all elements, similar to what has been done on this website: . Interestingly enough, they al ...

Having trouble selecting a URL tag that was dynamically created using JQuery by its class?

While working on implementing a TagCloud for my website, I encountered an issue with using JQuery to alert me about the selected link. Even though the links are being generated correctly and assigned the 'tagLink' class, when I attempt to determi ...

What are some ways to obscure the stored password in a database with characters like asterisks or other symbols

Currently, I am working on developing the admin features for my website and have been using this resource to assist with adding users: One issue that I have encountered is that when I added a password field, it appears in its stored form which is not idea ...

Learn how to display JSON data sent from the server on a webpage

I'm just starting out with jquery. I have an api called '/categories' that gives me a json object of categories. The response looks like this: [ { name: "Laptop deals", slug: "laptop-deals", imageURL: "image.jpg", id: 1 }, { name: "Fashion ...

What is the best way to target a specific item in a list using JavaScript in order to modify its appearance?

How can I modify the appearance of a specific li element when it is clicked? ...

Avoid duplication of elements in Angular applications

Currently, I have a component that loads animated divs based on a datasource. *Note: In the example, I've used a lot of <any> because I haven't finalized the model yet. apiService.ts dataArray: Observable<Array<any>>; constru ...

How to position a Bootstrap 4 button group at the bottom of a flexbox container

I am working with HTML code that looks like this: <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/> <div class="border d-flex align-items-baseline m-2 p-2" style="width: 400px"> < ...

Is there a way to fix the noticeable difference in how bold text appears between IE11 and Chrome?

When viewing some of the titles on in Google Chrome, they appear like this: https://i.sstatic.net/bKjNE.png However, when viewed in MSIE11, they look like this: https://i.sstatic.net/SJfKk.png The use of strong/bold/font-weight seems to have no effect ...