JQuery problem with toggling the menu icon

I have a toggle control that switches between a menu icon and an X when clicked, displaying an overlay nav as well.

The issue is: Clicking on any link in the overlay nav does not switch/toggle the X back to the default menu icon.


In the code below, I included

$('.cd-menu-icon').toggleClass('is-clicked'); $('.cd-header').toggleClass('menu-is-open');
(on lines 33, 34) to set the initial toggled state of the control.

jQuery(document).ready(function($){
    //if you change this breakpoint in the style.css file (or _layout.scss if you use SASS), don't forget to update this value as well
    var MQL = 1170;

    //primary navigation slide-in effect
    if($(window).width() > MQL) {
        var headerHeight = $('.cd-header').height();
        $(window).on('scroll',
        {
            previousTop: 0
        }, function () {
            var currentTop = $(window).scrollTop();
            //check if user is scrolling up
            if (currentTop < this.previousTop ) {
                //if scrolling up...
                if (currentTop > 0 && $('.cd-header').hasClass('is-fixed')) {
                    $('.cd-header').addClass('is-visible');
                } else {
                    $('.cd-header').removeClass('is-visible is-fixed');
                }
            } else {
                //if scrolling down...
                $('.cd-header').removeClass('is-visible');
                if( currentTop > headerHeight && !$('.cd-header').hasClass('is-fixed')) $('.cd-header').addClass('is-fixed');
            }
            this.previousTop = currentTop;
        });
    }

    //open/close primary navigation
    $('.cd-primary-nav-trigger').on('click', function(){
        $('.cd-menu-icon').toggleClass('is-clicked');
        $('.cd-header').toggleClass('menu-is-open');

        //in firefox transitions break when parent overflow is changed, so we need to wait for the end of the trasition to give the body an overflow hidden
        if( $('.cd-primary-nav').hasClass('is-visible') ) {
            $('.cd-primary-nav').removeClass('is-visible').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend',function(){
                $('body').removeClass('overflow-hidden');
            });
        } else {
            $('.cd-primary-nav').addClass('is-visible').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend',function(){
                $('body').addClass('overflow-hidden');
            });
        }
    });
});

Hoping for assistance on this matter.


To replicate: (use JsFiddle Example)

  1. Open menu (click 'hamgburger' icon box area)
  2. Click any link on the overlay, when it's presented. (e.g., "1", "2", "3", "4")
  3. Outcome: The overlay closes, but the menu icon does not toggle back.

Answer №1

It seems like you have successfully incorporated custom click events on the links labeled 1, 2, 3, 4 within the <script> tag at line 253 in the HTML section.

To enhance this functionality, consider replacing the existing <script> tag with the code snippet below:

<script>
  $('.cd-primary-nav a').on('click', function(){
  $('.cd-menu-icon').removeClass('is-clicked');
  $('.cd-header').removeClass('menu-is-open');
  $('.cd-primary-nav').removeClass('is-visible').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend',function(){
    $('body').removeClass('overflow-hidden');
  });
  });
</script>

For a live demo and testing, visit JSFiddle: https://jsfiddle.net/c9dj010v/

Answer №2

If you want a quick fix, consider adding a new event listener to capture clicks on your links.

$('.nav-overlay').click(function() {
    $('.cd-menu-icon').toggleClass('is-clicked');
});

The issue with your original code is that the hamburger icon will only change when directly clicked because the listener is only attached to that particular element.

Improved Fiddle Link

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

display hidden sections, not entire pages

Similar Question: Printing content of <div id=printarea></div> only? Hello everyone, We are all familiar with the window.print() function. If we have a div containing some content, how can we print it? ...

Navigating the path for the script src dynamically with the help of Angular.js and Javascript

I could really use some assistance. I am trying to dynamically set the js/css path name using Angular.js/Javascript/Jquery. Let me explain my code below. <script src="/crm/controller/productController.js" type="text/javascript"></script> Let& ...

Tips for merging jquery-1.10.2.js and jQuery-2.1.4.min.js simultaneously

Hey there! I've run into an issue where including jQuery-2.1.4.min.js and jquery-1.10.2.js together is causing the following error: custom.js:375 Uncaught TypeError: $(...).autocomplete is not a function If I remove jQuery-2.1.4.min.js, then I enc ...

Adding a Stripe pay button using Jquery append results in the return of an [object HTMLScriptElement

I'm currently working on dynamically adding the Stripe pay with card button through jQuery. I've decided to append it because I only want it to show up once a specific condition is met by the user, as well as due to the fact that the pricing can ...

Creating a website layout with two evenly sized columns, one of which can be scrolled through

I am facing a challenge with my HTML table that has one row and two columns. I want the two columns to have equal height, but achieving this has proven difficult using floats or flexbox. Therefore, I turned to using a HTML-table. The content in the left c ...

Tips for updating the font color of BarMenu in Blazorise

I am struggling to change the default font color of the bar menu to red in a razor page. Despite trying the code below, it doesn't seem to work: <Bar Breakpoint="Breakpoint.Desktop" Background="Background.Light" ThemeContr ...

Transmit an array in a post request with Node.js using the application/x-www-form-urlencoded content type

After attempting to send a post request to an API with post parameters as an array, I encountered difficulties. Here is how it can be done using cURL: curl http://localhost:3000/check_amounts -d amounts[]=15 \ -d amounts[]=30 I then tried to ach ...

Is it feasible to arrange <DIV>s based on their dates?

I encountered an issue while working with an ordering function. var $wrapper = $('#list'); $wrapper.find('.blogboxes').sort(function (a, b) { return +b.dataset.date - +a.dataset.date; }) .appendTo( $wrapper ); <script src="ht ...

The icons in Ionic do not appear on the Android emulator

While working on a Meteor/Angular/Ionic tutorial, I encountered the following issue: https://i.sstatic.net/gtNiV.png When using Nexus 6 simulator with Marshmallow screen, the Ionic icons are not appearing. The buttons are declared like this: < ...

Modify HTTP request headers within an Angular controller

Hi, I am trying to modify HTTP headers from within a controller. Although I know it can be done in the config using $httpProvider, that's not the approach I want to take. When I attempted this code snippet, I encountered an error saying "push of unde ...

Implement a jQuery loop that utilizes the fadeIn effect

Currently, I have a basic jQuery function in place to generate a small image slider: function gridhover() { $(".grid-item .slide-image").each(function(index) { $(this).delay(400*index).fadeIn(300); }); } $( ".grid-item" ).hover(function() ...

Ensuring uniformity in picture size while uploading images in Angular by resizing and including white borders

Currently, I am working with pictograms and I am looking to showcase all the pictograms that the user has in both grid list and grid tiles formats. However, the challenge lies in the fact that the aspect ratio of the pictograms varies significantly depen ...

Unable to open a directory in PHP using a passed value as it is not functioning properly

This is a snippet of my HTML and PHP code: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Gallery</title> <script src="js/jquery-1.7.2.min.js"></script> <script ...

Troublesome CSS media queries failing to meet expectations

I am facing an issue with two divs that are supposed to be 368px x 228px and sit next to each other. Despite my attempts to apply media queries and run tests, I am unable to resolve the issue. Can someone guide me on how to fix this? Below is my code: < ...

Jssor restricts the movement direction of slides

Currently, I have implemented two slides that transition smoothly from the first to the second. However, my superior has requested that they do not move backwards from the second slide back to the first. Unfortunately, upon researching the Jssor API, I hav ...

What is the best way to attach a tag or label onto multiple objects so that it remains facing the camera whenever the user interacts with the objects?

My goal is to create a tag or label that always faces the camera when a user clicks on an object, even if that object is rotated. How can I achieve this? I've been advised to use an Orthogonal camera, but I'm not sure how to do that. Additionall ...

Error when handling JSONP: Unexpected token colon in SyntaxError

Encountering a similar issue to the ones discussed in this JSON Uncaught SyntaxError thread and this JSONP Unexpected Token error thread. I attempted to fetch Divvy data through a JSONP call to avoid using a server for my simple front-end application. $. ...

Tips for validating date input in a TextBox using JQuery on an ASP.NET platform:

Here is some code I wrote: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="datetime.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <tit ...

Issue with table sorting functionality following relocation of code across pages

I had a working code that I was transferring from one webpage to another along with the CSS and JS files, but now it's not functioning properly. <head> <link type="text/css" rel="stylesheet" href="{{ STATIC_URL }}assets/css/style.css"> ...

Using jquery's next() function to bypass a label

I am facing an issue with a function that populates a second field based on the values in the first field. The problem arises when there is a label associated with the field, as the label becomes the next() element. As a result: function buildoptions( ...