on my website I have a mobile menu. When I click a link a link in the menu the menu doesn't disappear. From reading other posts I have a quite good idea what I have to do. But I don't get the code working, because I am completely new to javascript and probably just do something wrong.

The div I want to hide when clicking a link (in this same div) is defined with a class mobilemenuitems

As I already mentioned the links are within this div.

unfortunately I cannot add a class or an id to the links because I only have frontend access.

The website is here. http://test.vereinonline.org/HTC_Uhlenhorst/?module=*Tennis Please note that the menu button only appears on mobile devices (width < 1000px)

In this jsfiddle the Problem is scaled down to the root. http://jsfiddle.net/TheBB23/d6s3Ln50/3/

I am pretty sure that the problem is with the javascript:

document.getElementById(mobilemenuitems a).addEventListener('click', function(e) {
  • Please post the code you’re having trouble with. – paddyfields Oct 19 at 14:52

I believe you are trying to hide the div with class mobilemenuspace when any of the links inside it are clicked. To do so, you can use the following -

    if ($(this).parents('.mobilemenuspace').length) {

Working sample - http://jsfiddle.net/zv18xuhL/

A pure JS solution forked from your Fiddle -


  • Yes it works, the menu disappears but the link is not opened now. and another problem that I didn't think of appears now: The menu hides but the menu button which is animated still looks like the menu is open. Is it possible to trigger the function that rotates the menu button and shows the menu when clicking a link inside? – BB23 Oct 20 at 9:43
  • You'd want to remove the event.preventDefault() to get the links to work. To trigger the menu button function just fire its click event. Say the id of the button is menuroot you just need to do a $('#menuroot').click(); – xprilion Oct 20 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.