Can't trigger the close button click event from the Menu

can definition
can synonym
can verb
can y
can noun meaning
can band
cant
can't

I have this burger menu which I can't invoke the button onclick event function when I click it.

HTML code:

<button class="nav-aside-close"><i class="fa fa-times"></i></button>

JQuery code:

$('.nav-aside-close').on('click', function () {  
     console.log('test');  
     $('#nav-aside').removeClass('active');
     $('#nav').removeClass('shadow-active');
});

If I click any area outside the burger menu, it works. Below is the code which works:

$(document).click(function(event) {
        if (!$(event.target).closest($('#nav-aside')).length) {
            if ($('#nav-aside').hasClass('active')) {
                $('#nav-aside').removeClass('active');
                $('#nav').removeClass('shadow-active');
            } else {
                if ($(event.target).closest('.aside-btn').length) {
                    $('#nav-aside').addClass('active');
                    $('#nav').addClass('shadow-active');
                }
            }
        }
    });

Actual code I have uploaded it at http://js.findingsteve.net


If you're using Chrome, open up DevTools (F12) and do this on the Console tab and hit Enter:

getEventListeners(document.querySelector('button.nav-aside-close'))

If you see any click events registered, it should work.

Anyway, I noticed you are putting the main.js file on the <head> and not using jQuery.ready, so your click handler is essentially never attached since the DOM element is not ready by the time the script executes.

Solution:

Add the jQuery alias $ on the very first line of your JS file, that is a shorthand for jQuery.ready BTW.

$(function($) {
    "use strict"

    // Fixed Nav
    var lastScrollTop = 0;

Don't forget to also remove the jQuery function assignment from the end of the line, since it's no longer an IIFE.

    setStickyPos();

})(jQuery);

Alternative, you can keep everything as is and move your main.js file to the <body> element, right before the closing </body> tag. That is pretty much the same as having the scripts executed when all the elements above it have finished loading.

May I? Can I? - English Grammar - English, Can was the leading avant-garde rock group of the 70s. Can experimented with noise, synthesizers, non-traditional music, cut-and-paste techniques, and, most  Can definition, to be able to; have the ability, power, or skill to: She can solve the problem easily, I'm sure. See more.


It's probably because .nav-aside-close is hidden in the initial render.

Try this instead:

$("#nav").on("click", ".nav-aside-close", function() {
  $("#nav-aside").removeClass("active");
  $("#nav").removeClass("shadow-active");
});

Use can in a sentence, Can may refer to: Contents. 1 Containers; 2 Music; 3 Other; 4 See also. Containers[edit]. Aluminum can · Drink can · Oil can · Steel and tin cans · Trash can  Define can. can synonyms, can pronunciation, can translation, English dictionary definition of can. to be able to, have the power or skill to: I can take a bus to the


Based in @Ana Liza Pandac comment

It should be:

$("#nav").on("click", "#nav-aside .nav-aside-close", function() {
  $("#nav-aside").removeClass("active");
  $("#nav").removeClass("shadow-active");
});

Your tree is: nav->nav-aside and obj with class nav-aside-close

The difference is on: $("#nav").on("click", "#nav-aside .nav-aside-close", function()

CAN (noun) definition and synonyms, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in adjectival use) couth). can definition: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more.


Can, can meaning: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more. 110 synonyms of can from the Merriam-Webster Thesaurus, plus 97 related words, definitions, and antonyms. Find another word for can. Can: to bring (as an action or operation) to an immediate end.


Can, 1.4Used to indicate that something is typically the case. 'antique clocks can seem out of place in modern homes'. More example sentences. Can means someone or something knows how to, is able to, is likely to or has the right to do something. An example of can is someone knowing how to play the piano. An example of can is a cat being able to paint. An example of can is a car that usually starts.


The Official CAN / Spoon Records Website, can. (kæn). n. 1. a container, esp for liquids, usually of thin sheet metal: a petrol can;  Looking for online definition of CAN or what CAN stands for? CAN is listed in the World's largest and most authoritative dictionary database of abbreviations and acronyms The Free Dictionary