Activate unresponsive state when clicked

Struggling to implement a mobile responsive menu, my primary issue is the navigation buttons not toggling upon clicking. Here's what I have so far in terms of HTML and JQuery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>
    $(document).ready(function() {
        $('span.nav-btn').click(function() {
            $('ul.nav').slideToggle();
        });
    });
    </script>
</head>
<body>
    <header>
        <h1>Hamburger MENU</h1>
    </header>
        <nav>
            <span class="nav-btn"></span>
            <ul class="hamburger">
                <li><a href="#">Desktop 1</a></li>
                <li><a href="#">Desktop 2</a></li>
                <li><a href="#">Desktop 3</a></li>
                <li><a href="#">Desktop 4</a></li>
            </ul>
        </nav>

Here's the corresponding CSS snippet:

header {
background-color: #23DA43;
}

h1 {
width: 100%;
text-align: center;
padding: 10px 0 10px 0;
}

.hamburger {
background-color: #333;
color: #fff;
margin: 0;
list-style: none;
text-align: center;
}

.hamburger li {
display: inline-block;
}

.hamburger li a {
text-decoration: none;
font-size: 40px;
color: #fff;
}


@media screen and (max-width: 900px) {

.hamburger {
    text-align: left;
    display: none;
}

.hamburger li {
    display: block;
}

.nav-btn {
    display: block;
    background-color: #333;
    color: #fff;
    font-size: 40px;
    text-align: center;
    cursor: pointer;
}

.nav-btn:before {
    content: "Menu";
}

}

Your assistance is greatly appreciated. Regards, Alessandro

Answer №1

Revise your JavaScript code to resemble the following:

 $(document).ready(function (){
    $('.nav-btn').on("click", function() {
        $('.hamburger').slideToggle("slow");
    });
});

Your previous code wasn't functioning correctly due to using .ready(function){}); instead of .ready(function() { });

Answer №2

Uh oh, there seems to be an issue:

 $(document).ready(function) {
                $('span.nav-btn').click(function() {
                    $('ul.hamburger').slideToggle();
                });
            });

Here is the solution:

<!DOCTYPE html>
<html>
    <head>
        <style>

            header {
            background-color: #23DA43;
            }

            h1 {
            width: 100%;
            text-align: center;
            padding: 10px 0 10px 0;
            }

            .hamburger {
            background-color: #333;
            color: #fff;
            margin: 0;
            list-style: none;
            text-align: center;
            }

            .hamburger li {
            display: inline-block;
            }

            .hamburger li a {
            text-decoration: none;
            font-size: 40px;
            color: #fff;
            }


            @media screen and (max-width: 900px) {

                .hamburger {
                    text-align: left;
                    display: none;
                }

                .hamburger li {
                    display: block;
                }

                .nav-btn {
                    display: block;
                    background-color: #333;
                    color: #fff;
                    font-size: 40px;
                    text-align: center;
                    cursor: pointer;
                }

                .nav-btn:before {
                    content: "Menu";
                }

            }
        </style>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
            <script>
            $(document).ready(function() {
                $('span.nav-btn').click(function() {
                    $('ul.hamburger').slideToggle();
                });
            });
        </script>
    </head>
    <body>
        <header>
            <h1>Hamburger MENU</h1>
        </header>
        <nav>
            <span class="nav-btn"></span>
            <ul class="hamburger">
                <li><a href="#">Desktop 1</a></li>
                <li><a href="#">Desktop 2</a></li>
                <li><a href="#">Desktop 3</a></li>
                <li><a href="#">Desktop 4</a></li>
            </ul>
        </nav>
     </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

Initiating an audio call exclusively via SimpleWebRTC

I'm currently utilizing a jQuery plugin for WebRTC found here. My goal is to initiate an audio call only, but I am struggling to find a function within the plugin that allows for this. The code snippet I am using with autoRequestMedia set to false is ...

Discovered a critical vulnerability that requires your attention. Execute `npm audit fix` to resolve the issue, or use `npm audit` for more information

After attempting to install mysql using 'npm install mysql', I encountered an error message stating: "found 1 critical severity vulnerability run npm audit fix to fix them, or npm audit for details" Following this, I proceeded with 'npm au ...

Exploring the process of extracting/parsing HTML with Microdata

Hello, I am relatively new to the concept of Microdata. I currently have an HTML string that contains Microdata and I am interested in exploring the possibility of extracting the necessary information dynamically using Microdata with JavaScript or jQuery. ...

Is there a way to navigate by scrolling, moving a centrally-positioned SVG along a path while also resizing the SVG?

After following the instructions in this post about resizing SVGs, I managed to keep the red square on the path while resizing the SVG. However, a new issue arises when scrolling down - the red square is not moving to stay positioned at the center of the w ...

Transferring data from a form to a function in JavaScript

Hey there! I've been working on a form that sends a value to a new window, but for some reason, the value val2 is showing up as null in the new window instead of being passed. Here's my code: function sendValue(value) { ViewImg = window.ope ...

PHP script to populate a table with images based on certain conditions

I have a table in HTML that displays results from a SQL database on the right hand side. The code snippet for populating each cell is as shown below: <tr> <td>House</td> <td><?php echo $row_buyerdetails['tod_hous ...

Within a div element, there are links that I would like to hover over to emphasize the text

Hey there, I'm looking to make the text change color when you hover over the entire div. Currently, only the background changes color, but I want the text to as well. Thank you for your assistance! .shadow-box .service-box {text-align:center;borde ...

What is the best way to ensure that navigation takes up the full width of the screen in pixels when

Trying to use jQuery to set a specific pixel width for my Bootstrap navbar that spans the entire width of the browser window. However, encountering some bugs as the width needs to be recalculated whenever the browser is resized. Currently, I have this cod ...

Rows nested within the Bootstrap grid system

Looking to implement a nested grid within a parent grid in Bootstrap 3.3.7. Check out the HTML below: Parent component <div> <div class="row"> <div class="col-md-3 border">.col-md-3</div> // <div class="col-md-9 ...

The `.append()` function includes HTML content as plain text

I am using JavaScript to dynamically add HTML elements to my webpages. I have created a loop that iterates through all the projects, each containing multiple pictures. The first step involves generating the project title and adding it within a div element ...

Adjust the text color of a div element by clicking a button with the help of JavaScript

I am trying to change the font color of my div when a button is clicked using JavaScript. The two buttons I have are 'Green' and 'Red'. <button type="button" class="green">Green</button> <button type="button" class="red" ...

Stopping a velocity.js animation once it has completed: is it possible?

For the pulsating effect I'm creating using velocity.js as a fallback for IE9, refer to box2 for CSS animation. If the mouse leaves the box before the animation is complete (wait until pulse expands and then move out), the pulsating element remains vi ...

Does CSS include a shadow child operator?

Did you know that Google Chrome has a special feature called the shadow descendant combinator /deep/? This unique combinator allows for the selection of elements within shadow nodes. body div {...} // this doesn't target descendant divs inside shadow ...

How can we use withStyles to customize the styling of a selected ListItem in ReactJS and Material UI?

I am seeking assistance in changing the style of a specific ListItem, similar to what is demonstrated in this Codesandbox. In the Codesandbox example, a global CSS stylesheet is utilized, but I prefer to implement the changes using the withStyle techniqu ...

Lint error: Unrecognized attribute 'text-fill-color' in CSS (unknown properties)

My navigation bar isn't functioning, and I can't figure out why. It seems like it might be related to this snippet of code: -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; text-fill-color: trans ...

What method can be used to effectively track markups within a string?

If this question has been posed differently before, please direct me to it as I couldn't locate it in my search results. I am interested in parsing text for various mark-ups, similar to those found on SO. eg. * some string for bullet list eg. *som ...

Is it possible to add and delete DIVs simply by clicking on a link?

By utilizing a select dropdown list and appending the HTML code stored in the variable "code" (resembling <div>...</div>) right before the end of the div with ID mydiv using $(code).appendTo('#mydiv');. Within these dynamically added ...

Tips for positioning a Div element in the center of a page with a full-page background

I am trying to center align my div containing login information on the webpage. The static values seem to work fine, but I am looking for a way to position it dynamically. Additionally, the background is only covering a strip with the height of the div... ...

Selenium Scrolling: Improving Web Scraping Efficiency with Incomplete Data Extraction

I have been attempting to extract product data from a website that utilizes JavaScript to dynamically render HTML content. Despite using Selenium, implementing scrolling functionality to reach the end of the page, and allowing time for the page to reload, ...

adjusting the size of the sidebar at the top to ensure that it does not overlap the top menu

I have a sidebar on my page that opens vertically when I click a button. The sidebar covers the company logo and name, so I want to make it smaller. Here is the code for the sidebar: <body> <nav class="navbar navbar-dark bg-dark" ...