Pierre
Pierre

Reputation: 35306

Clicking an <img> preventing the activation of the <a>

I'm trying to implement a bookmarklet where the user will click one <img/> and will be redirected to another page where he will annotate the image.

If the image is inserted within a HTML anchor:

<a href="http://anywhere.org"><img src=""http://anywhere.org/image.png"/></a>, 

Can I prevent the anchor to be activated? I tried

event.stopPropagation();

and/or

event.preventDefault();

but it didn't work

Upvotes: 1

Views: 50

Answers (2)

naikus
naikus

Reputation: 24472

You should use event.cancelBubble and return false (for IE).

But your method works fine in FF.

  var stop = function(evt)   {
     evt = evt || window.event;


     if(typeof(evt.stopPropagation) === "function") {
        evt.stopPropagation();
     }

     if(typeof(evt.preventDefault) === "function")   {
        evt.preventDefault();
     }

     // this is for IE6
     evt.cancelBubble = true;         
     return false;
  };

Upvotes: 1

Frankie
Frankie

Reputation: 25165

First you can check if the event can be cancelated or not using:

var bool = event.cancelable;

And you can always try the classic

return false:

That will pretty much stop it.

Upvotes: 2

Related Questions