The whole navigation bar vanishing unexpectedly after executing a jQuery click function on a mobile-friendly website

I am in the process of designing a responsive website that features a collapsible navigation bar specifically for small screens.

However, I encountered an issue where the nav bar disappears when I resize the screen to a larger size after toggling the menu on and off. Although this problem may only affect a small group who frequently adjust their browser window size and interact with the menu option.

I feel like there is some logical error that I am overlooking, but I just can't seem to pinpoint it.

Below is the code snippets:

    <nav id="mainNav">
        <img id="menu" src="menu.png" alt="menu icon" width="50" />
        <ul id="slide">
            <li><a href="#news">News</a></li>
            <li><a href="#calendar">Calendar</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#sponsors">Sponsors</a></li>
            <li><a href="#contact">Contact</a></li>
        </ul>
    </nav>

CSS:

#mainNav {
    background-color: #333;
    width: 100%;
    z-index: 500;
}

#menu {
    display: none;
}

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

    #menu {
        display: block;
    }

    #slide {
        display: none;
    }

jQuery:

$(function(){

    "use strict";

        $("#menu").click(function(){ 
            if ($("#slide").css("display") === "block") {
                $("#slide").css({ "display": "none" });
            }
            else if($("#slide").css("display") === "none") {
                $("#slide").css({ "display": "block" });
            }
        }
    );
});

Answer №1

To modify the script, do the following:

     $(function () {
  $("#menu").click(function () {
    if ($("#slide").is(':hidden')) {
      $("#slide").show();
    } else {
      $("#slide").hide();
    }
  });
  $(window).resize(function () {
    if ($(window).width() > 500) {
      $("#slide").show();
    } else {
      $("#slide").hide();
    }
  });
});

View the code on jsfiddle: https://jsfiddle.net/b7v14zkf/

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

Completely alter the appearance of an <img> element using CSS without modifying the src attribute

Hey there! I currently have an image in the img src="image" tag. Is there a method within CSS to completely replace that image? I am unable to modify the JavaScript or HTML. I attempted using background-image, but the original image is still b ...

Steps to transfer the content of a label when the onclick event occurs

Seeking advice on how to send the dynamically varying value of a label upon clicking an anchor tag. Can anyone recommend the best approach to passing the label value to a JavaScript function when the anchor is clicked? Here is a sample code snippet: < ...

How can I use a button created with jQuery's .html() method to conceal a <div>?

I am facing an issue with implementing a simple banner that should appear in an empty element only when specific values are returned by an Ajax call. In the banner, I want to include a Bootstrap button that hides the entire div when clicked. Due to my la ...

Eliminate the jQuery function call in JavaScript

I am working with a file.js that looks like this: // TICautocapture.js var TICautocapture = (function(){ var lib = {...} var error_handler; var handleError = (error_code, error_callback) => {...} function autocapture(container, options){...} ...

Is there a way to make my sticky sidebar automatically scroll back up when the page reaches the footer?

I'm facing a common issue on StackOverflow, but the solutions I've come across so far haven't completely resolved my problem. To understand the issue better, you can take a look at this fiddle that I found referenced here. My challenge lies ...

position blocks next to each other within a container

Thank you for all the hard work! I've been struggling to figure out how to align blocks next to each other within a div. It may not be that difficult, but I just can't seem to find an elegant solution... Here is the HTML code snippet: <div ...

Styling based on conditions, hovering effects, and implementing ReactJS

One issue I am facing is with an anchor element within a <div>. The anchor has conditional background styling, which seems to override the a:hover style. Whether I have conditional or fixed coloring, removing the background-style from component.js al ...

Simulating the behavior of display blocks

HTML similar to the example below is working perfectly, however, there seems to be an issue with Sharepoint 2013's editor. When trying to edit the link text within a block that has 'display: block' or 'float', it becomes impossible ...

Interacting with a PNG file using a border radius in Internet Explorer 9 is causing issues with its transparency

Encountering an issue with IE9 where a white to gray gradient box appears around the transparent PNG on hover/selection. There is also a border radius applied to the thumbnail. Any ideas on how to fix this problem? Here is an example of the issue: https ...

When you click on either the Main menu or the Search box in the image, only one menu should open at a time while the other closes

View the image of how the menu looks when both menus are opened. When clicking on each menu separately, both menus appear instead of closing one when the other is clicked. How can I achieve this? $('.toggle-sm-nav, .js-toggle-sm-navigation').c ...

Uploading files asynchronously with Erlang Cowboy using AJAX

I am facing an issue with AJAX file uploading to Erlang Cowboy. I am able to stream the file upload with mutltpart/form-data. <form method="post" enctype="multipart/form-data"> However, I am unable to stream the file uploading using AJAX because it ...

Tips for successfully including special characters in a query string using asp.net and jquery

Is there a way to pass %20 in the Query string without it being interpreted as a space in the code behind? I need to pass query strings using ASP.NET and also from JavaScript. Any suggestions on how to achieve this? Thank you in advance for your help. ...

What's the best way to ensure that the iframe resolution adjusts dynamically to perfectly fit within its container?

iframe-screenshot Displayed in the image are two iframes, but at times I may need to display three. The aim is to ensure that all iframes are responsive within their containers. Below is my existing CSS styling: iframe { width: 100%; height: 1 ...

My goal is to dynamically assign a class to an iframe element by identifying a specific class contained within the html of the iframe

I need assistance with adding a class to an iframe only if the body tag has a specific class name. Here is my attempt at writing some code: <iframe class='iframeContent' src='' width='100%' height='' frameborder= ...

Stylish popup image with a link to an external source

Currently, I am utilizing Fancybox 3 and Cookie in order to display hidden content once the page has finished loading. The concealed content consists of both an image and an external link. I am encountering difficulties with regards to the functionality ...

Capturing jQuery ajax requests in Angular

Within my Angular application, I am utilizing the integrated $.couch API from CouchDB. My goal is to intercept every ajax request, regardless of whether it originates from Angular's $http service or jQuery's $.ajax (which is utilized by $.couch). ...

The top scrolling behavior on click applies exclusively to the first set of Bootstrap 5 tabs

I am experiencing an issue with my HTML webpage that contains multiple BS5 Nav Tabs. The first tab is not functioning properly, while all the other tabs are working smoothly. When I click on the first BS5 Nav Tab, the page scrolls to the top with the addre ...

Trigger file download with ajax and php

I have been using an image picker plugin to select an image and then force it to download. Initially, I hard coded the PHP which worked perfectly fine. The download pop-up appeared and I was able to view the file without any issues. However, when trying to ...

The Codeigniter submission form using jQuery ajax() encountered an error

Can someone please help me out? I've gone through numerous tutorials on CodeIgniter from various websites, including Stack Overflow, but haven't found a solution to my problem. Guys, can you please teach me? This is the jQuery ajax function code ...

Which Javascript/Css/HTML frameworks and libraries would you suggest using together for optimal development?

Interested in revamping my web development process with cutting-edge libraries, but struggling to navigate the vast array of tools available. The challenge lies in finding a harmonious blend of various technologies that complement each other seamlessly. I& ...