Radical
Radical

Reputation: 1073

Stopping click event using mousedown only works when triggering an alert

I'm trying to prevent the defined onclick events of the children of an element being run by returning false from the mousedown event on the container like so:

submit_container.mousedown(function(event){
    return false;
});

If I write my code like this, it doesn't work. However, if I add an alert first, the click events are in fact stopped:

submit_container.mousedown(function(event){
    alert('Alert');
    return false;
});

The same issue was alluded to here, but sadly none of the answers address this.

EDIT: JSfiddle example. I want the behaviour of the second link, but without having to trigger an alert

Upvotes: 2

Views: 3101

Answers (2)

KpTheConstructor
KpTheConstructor

Reputation: 3291

The mousedown is working for the container class . Your problem is that a href has a click event attached to it not mouse down. So you should be using onclick on the container or the href as event Propagation will occur and bubble up to the parent . You can still stop this from happening .

Example.

submit_container.click(function(event){
    event.stopPropagation();
    event.preventDefault();
    return false;
});

Hope this helps

Upvotes: 2

Andrey
Andrey

Reputation: 1553

Are you using jQuery? Try with a "preventDefault" method:

submit_container.mousedown(function(event){
    event.preventDefault();
    return false;
});

Upvotes: 0

Related Questions