Scipion
Scipion

Reputation: 11888

2 actions linked to the same onClick function

I have a dropdown which closes when you click on it. But, the dropdown contains one a link which allows to download a file. If I add to my dropdown :

$('.dropdown').click(function (e) {
    e.preventDefault();
});

It will prevent it to be closed when you click on it but it will also prevent the download from happening. Is there a better way to handle the event to prevent the dropdown from being closed, but still allowing the a links to work ?

Thanks

Upvotes: 2

Views: 56

Answers (1)

Charlie
Charlie

Reputation: 23778

You should use e.stopPropagation() instead of e.preventDefault()

The latter prevents the default action from taking place. So it prevents the click on the link too.

But the former only...

Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.

It will still let your link to be clicked - but cuts the propagation down from the click event of the drop down.

Upvotes: 1

Related Questions