Anchor only firing event when just a #











up vote
-2
down vote

favorite












I am struggling to find out why my click event is not firing. I have a menu which is overlayed over the main content. When the anchor is clicked, it should close the overlay and scroll to the element.



I can see the scrolling work (the overlay is partially transparent), but the event is not firing to close the overlay.



If I click the Contact Us link (which is currently just a #), the event is fired and the overlay does close.






(function($) {
$('#mobile-menu-toggle').click(function() {
if ($(this).hasClass('toggle-open')) {
$("body").toggleClass("mobile-menu-open");
$("#mobile-menu").fadeOut();
$(this).toggleClass("toggle-open");
} else {
$("body").toggleClass("mobile-menu-open");
$("#mobile-menu").fadeIn();
$(this).toggleClass("toggle-open");
}
});

$(document).on('click', '.ToggleMenu', function(event) {
alert("Clicked");
$("button.menu-toggle").trigger('click');
});

})(jQuery);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="mobile-menu-toggle" class="menu-toggle" aria-controls="primary-menu" aria-expanded="false">
<span class="menu-label bdn-gsk"></span>
<div class="burger-icon">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</button>

<nav id="mobile-menu" class="mobile-menu" style="display:none;">
<div class="site-branding">
<h1 class="site-title bdn-gsk"><a href="#" rel="home">Menu Heading</a></h1>
</div>
<div class="menu-inner-wrap">
<div class="menu-wrap serif">
<div class="menu-main-menu-container">
<ul id="primary-menu" class="menu">
<li class="ToggleMenu"><a href="#Home">Home</a></li>
<li class="ToggleMenu"><a href="#About">About</a></li>
<li class="ToggleMenu"><a href="#WhatsOn">What&apos;s On</a></li>
<li class="ToggleMenu"><a href="#OurWork">Our Work</a></li>
<li class="ToggleMenu"><a href="#">Contact</a></li>
</ul>
</div>
</div>
</div>
</nav>





This is in a Wordpress site.



Thank you










share|improve this question




















  • 1




    what kind of link is #Home?
    – guradio
    Nov 12 at 0:22






  • 2




    Consider using consistent indentation in your code - it'll make debugging much easier (not only for others, but for you as well)
    – CertainPerformance
    Nov 12 at 0:29






  • 2




    I moved your code into a snippet and, without any css, it seems like it may be working... care to verify that or add your styles, if there is anything covering the menu the click may not register...
    – lucascaro
    Nov 12 at 0:39










  • Thanks - I will check all css and see if I can find out why it is not working
    – pbs
    Nov 12 at 9:07















up vote
-2
down vote

favorite












I am struggling to find out why my click event is not firing. I have a menu which is overlayed over the main content. When the anchor is clicked, it should close the overlay and scroll to the element.



I can see the scrolling work (the overlay is partially transparent), but the event is not firing to close the overlay.



If I click the Contact Us link (which is currently just a #), the event is fired and the overlay does close.






(function($) {
$('#mobile-menu-toggle').click(function() {
if ($(this).hasClass('toggle-open')) {
$("body").toggleClass("mobile-menu-open");
$("#mobile-menu").fadeOut();
$(this).toggleClass("toggle-open");
} else {
$("body").toggleClass("mobile-menu-open");
$("#mobile-menu").fadeIn();
$(this).toggleClass("toggle-open");
}
});

$(document).on('click', '.ToggleMenu', function(event) {
alert("Clicked");
$("button.menu-toggle").trigger('click');
});

})(jQuery);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="mobile-menu-toggle" class="menu-toggle" aria-controls="primary-menu" aria-expanded="false">
<span class="menu-label bdn-gsk"></span>
<div class="burger-icon">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</button>

<nav id="mobile-menu" class="mobile-menu" style="display:none;">
<div class="site-branding">
<h1 class="site-title bdn-gsk"><a href="#" rel="home">Menu Heading</a></h1>
</div>
<div class="menu-inner-wrap">
<div class="menu-wrap serif">
<div class="menu-main-menu-container">
<ul id="primary-menu" class="menu">
<li class="ToggleMenu"><a href="#Home">Home</a></li>
<li class="ToggleMenu"><a href="#About">About</a></li>
<li class="ToggleMenu"><a href="#WhatsOn">What&apos;s On</a></li>
<li class="ToggleMenu"><a href="#OurWork">Our Work</a></li>
<li class="ToggleMenu"><a href="#">Contact</a></li>
</ul>
</div>
</div>
</div>
</nav>





This is in a Wordpress site.



Thank you










share|improve this question




















  • 1




    what kind of link is #Home?
    – guradio
    Nov 12 at 0:22






  • 2




    Consider using consistent indentation in your code - it'll make debugging much easier (not only for others, but for you as well)
    – CertainPerformance
    Nov 12 at 0:29






  • 2




    I moved your code into a snippet and, without any css, it seems like it may be working... care to verify that or add your styles, if there is anything covering the menu the click may not register...
    – lucascaro
    Nov 12 at 0:39










  • Thanks - I will check all css and see if I can find out why it is not working
    – pbs
    Nov 12 at 9:07













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I am struggling to find out why my click event is not firing. I have a menu which is overlayed over the main content. When the anchor is clicked, it should close the overlay and scroll to the element.



I can see the scrolling work (the overlay is partially transparent), but the event is not firing to close the overlay.



If I click the Contact Us link (which is currently just a #), the event is fired and the overlay does close.






(function($) {
$('#mobile-menu-toggle').click(function() {
if ($(this).hasClass('toggle-open')) {
$("body").toggleClass("mobile-menu-open");
$("#mobile-menu").fadeOut();
$(this).toggleClass("toggle-open");
} else {
$("body").toggleClass("mobile-menu-open");
$("#mobile-menu").fadeIn();
$(this).toggleClass("toggle-open");
}
});

$(document).on('click', '.ToggleMenu', function(event) {
alert("Clicked");
$("button.menu-toggle").trigger('click');
});

})(jQuery);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="mobile-menu-toggle" class="menu-toggle" aria-controls="primary-menu" aria-expanded="false">
<span class="menu-label bdn-gsk"></span>
<div class="burger-icon">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</button>

<nav id="mobile-menu" class="mobile-menu" style="display:none;">
<div class="site-branding">
<h1 class="site-title bdn-gsk"><a href="#" rel="home">Menu Heading</a></h1>
</div>
<div class="menu-inner-wrap">
<div class="menu-wrap serif">
<div class="menu-main-menu-container">
<ul id="primary-menu" class="menu">
<li class="ToggleMenu"><a href="#Home">Home</a></li>
<li class="ToggleMenu"><a href="#About">About</a></li>
<li class="ToggleMenu"><a href="#WhatsOn">What&apos;s On</a></li>
<li class="ToggleMenu"><a href="#OurWork">Our Work</a></li>
<li class="ToggleMenu"><a href="#">Contact</a></li>
</ul>
</div>
</div>
</div>
</nav>





This is in a Wordpress site.



Thank you










share|improve this question















I am struggling to find out why my click event is not firing. I have a menu which is overlayed over the main content. When the anchor is clicked, it should close the overlay and scroll to the element.



I can see the scrolling work (the overlay is partially transparent), but the event is not firing to close the overlay.



If I click the Contact Us link (which is currently just a #), the event is fired and the overlay does close.






(function($) {
$('#mobile-menu-toggle').click(function() {
if ($(this).hasClass('toggle-open')) {
$("body").toggleClass("mobile-menu-open");
$("#mobile-menu").fadeOut();
$(this).toggleClass("toggle-open");
} else {
$("body").toggleClass("mobile-menu-open");
$("#mobile-menu").fadeIn();
$(this).toggleClass("toggle-open");
}
});

$(document).on('click', '.ToggleMenu', function(event) {
alert("Clicked");
$("button.menu-toggle").trigger('click');
});

})(jQuery);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="mobile-menu-toggle" class="menu-toggle" aria-controls="primary-menu" aria-expanded="false">
<span class="menu-label bdn-gsk"></span>
<div class="burger-icon">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</button>

<nav id="mobile-menu" class="mobile-menu" style="display:none;">
<div class="site-branding">
<h1 class="site-title bdn-gsk"><a href="#" rel="home">Menu Heading</a></h1>
</div>
<div class="menu-inner-wrap">
<div class="menu-wrap serif">
<div class="menu-main-menu-container">
<ul id="primary-menu" class="menu">
<li class="ToggleMenu"><a href="#Home">Home</a></li>
<li class="ToggleMenu"><a href="#About">About</a></li>
<li class="ToggleMenu"><a href="#WhatsOn">What&apos;s On</a></li>
<li class="ToggleMenu"><a href="#OurWork">Our Work</a></li>
<li class="ToggleMenu"><a href="#">Contact</a></li>
</ul>
</div>
</div>
</div>
</nav>





This is in a Wordpress site.



Thank you






(function($) {
$('#mobile-menu-toggle').click(function() {
if ($(this).hasClass('toggle-open')) {
$("body").toggleClass("mobile-menu-open");
$("#mobile-menu").fadeOut();
$(this).toggleClass("toggle-open");
} else {
$("body").toggleClass("mobile-menu-open");
$("#mobile-menu").fadeIn();
$(this).toggleClass("toggle-open");
}
});

$(document).on('click', '.ToggleMenu', function(event) {
alert("Clicked");
$("button.menu-toggle").trigger('click');
});

})(jQuery);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="mobile-menu-toggle" class="menu-toggle" aria-controls="primary-menu" aria-expanded="false">
<span class="menu-label bdn-gsk"></span>
<div class="burger-icon">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</button>

<nav id="mobile-menu" class="mobile-menu" style="display:none;">
<div class="site-branding">
<h1 class="site-title bdn-gsk"><a href="#" rel="home">Menu Heading</a></h1>
</div>
<div class="menu-inner-wrap">
<div class="menu-wrap serif">
<div class="menu-main-menu-container">
<ul id="primary-menu" class="menu">
<li class="ToggleMenu"><a href="#Home">Home</a></li>
<li class="ToggleMenu"><a href="#About">About</a></li>
<li class="ToggleMenu"><a href="#WhatsOn">What&apos;s On</a></li>
<li class="ToggleMenu"><a href="#OurWork">Our Work</a></li>
<li class="ToggleMenu"><a href="#">Contact</a></li>
</ul>
</div>
</div>
</div>
</nav>





(function($) {
$('#mobile-menu-toggle').click(function() {
if ($(this).hasClass('toggle-open')) {
$("body").toggleClass("mobile-menu-open");
$("#mobile-menu").fadeOut();
$(this).toggleClass("toggle-open");
} else {
$("body").toggleClass("mobile-menu-open");
$("#mobile-menu").fadeIn();
$(this).toggleClass("toggle-open");
}
});

$(document).on('click', '.ToggleMenu', function(event) {
alert("Clicked");
$("button.menu-toggle").trigger('click');
});

})(jQuery);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="mobile-menu-toggle" class="menu-toggle" aria-controls="primary-menu" aria-expanded="false">
<span class="menu-label bdn-gsk"></span>
<div class="burger-icon">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</button>

<nav id="mobile-menu" class="mobile-menu" style="display:none;">
<div class="site-branding">
<h1 class="site-title bdn-gsk"><a href="#" rel="home">Menu Heading</a></h1>
</div>
<div class="menu-inner-wrap">
<div class="menu-wrap serif">
<div class="menu-main-menu-container">
<ul id="primary-menu" class="menu">
<li class="ToggleMenu"><a href="#Home">Home</a></li>
<li class="ToggleMenu"><a href="#About">About</a></li>
<li class="ToggleMenu"><a href="#WhatsOn">What&apos;s On</a></li>
<li class="ToggleMenu"><a href="#OurWork">Our Work</a></li>
<li class="ToggleMenu"><a href="#">Contact</a></li>
</ul>
</div>
</div>
</div>
</nav>






jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 0:38









lucascaro

3,38611530




3,38611530










asked Nov 12 at 0:16









pbs

139113




139113








  • 1




    what kind of link is #Home?
    – guradio
    Nov 12 at 0:22






  • 2




    Consider using consistent indentation in your code - it'll make debugging much easier (not only for others, but for you as well)
    – CertainPerformance
    Nov 12 at 0:29






  • 2




    I moved your code into a snippet and, without any css, it seems like it may be working... care to verify that or add your styles, if there is anything covering the menu the click may not register...
    – lucascaro
    Nov 12 at 0:39










  • Thanks - I will check all css and see if I can find out why it is not working
    – pbs
    Nov 12 at 9:07














  • 1




    what kind of link is #Home?
    – guradio
    Nov 12 at 0:22






  • 2




    Consider using consistent indentation in your code - it'll make debugging much easier (not only for others, but for you as well)
    – CertainPerformance
    Nov 12 at 0:29






  • 2




    I moved your code into a snippet and, without any css, it seems like it may be working... care to verify that or add your styles, if there is anything covering the menu the click may not register...
    – lucascaro
    Nov 12 at 0:39










  • Thanks - I will check all css and see if I can find out why it is not working
    – pbs
    Nov 12 at 9:07








1




1




what kind of link is #Home?
– guradio
Nov 12 at 0:22




what kind of link is #Home?
– guradio
Nov 12 at 0:22




2




2




Consider using consistent indentation in your code - it'll make debugging much easier (not only for others, but for you as well)
– CertainPerformance
Nov 12 at 0:29




Consider using consistent indentation in your code - it'll make debugging much easier (not only for others, but for you as well)
– CertainPerformance
Nov 12 at 0:29




2




2




I moved your code into a snippet and, without any css, it seems like it may be working... care to verify that or add your styles, if there is anything covering the menu the click may not register...
– lucascaro
Nov 12 at 0:39




I moved your code into a snippet and, without any css, it seems like it may be working... care to verify that or add your styles, if there is anything covering the menu the click may not register...
– lucascaro
Nov 12 at 0:39












Thanks - I will check all css and see if I can find out why it is not working
– pbs
Nov 12 at 9:07




Thanks - I will check all css and see if I can find out why it is not working
– pbs
Nov 12 at 9:07

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254552%2fanchor-only-firing-event-when-just-a%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254552%2fanchor-only-firing-event-when-just-a%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine