0

I have a navigation menu that I need to hide after a link has been clicked. Here's my code:

jQuery("#nav a").click(function(e){
    e.preventDefault();
    var body = jQuery('body');
    if (body.hasClass('display-header'))
    {
        body.removeClass('display-header');
        jQuery('.overlay').fadeOut(200);
    }
}); 

The remove class works fine but the link won't go to the right section. (Imagine a single page with anchor links.)

How would you go about this?

Thank you.

Chris.S
  • 3
  • 1

2 Answers2

1

I think that you should try to use e.stopPropagation() for do what you want.

Read this answer: event.preventDefault() vs. return false

See you.

Community
  • 1
  • 1
  • This would depend solely on what was around the anchor, but there is no obvious reason to use `stopPropagation` here. Just removing `preventdefault()` would likely suffice. – iCollect.it Ltd Jan 14 '16 at 15:06
  • Perfect. Thank you very much. I removed the prevent default and all is good :) – Chris.S Jan 14 '16 at 15:10
0

Try removing the:

e.preventDefault();

and see if that helps.

from the documnetation : api.jquery.com/event.preventdefault/

"For example, clicked anchors will not take the browser to a new URL. We can use event.isDefaultPrevented() to determine if this method has been called by an event handler that was triggered by this event."

Also, if you could provide a full HTML & javascript example that reproduces your issue - it would help greatly !

JanivZ
  • 2,265
  • 3
  • 25
  • 31