2

I want to disable click event for image using image id eg. for id12 i want to disabled click event . i tried using unbind but it seems it only works if event is binded using j query?

KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52

6 Answers6

7
$("#id12").on('click',function(e){
  e.preventDefault();
});
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
PSR
  • 39,804
  • 41
  • 111
  • 151
1
$("#id12").on('click',function(){
  return false;
});
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
1
$("#id12").click(function () { return false; });
Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
  • 2
    Actually, if I remember correctly this works, why was this downvoted? – Benjamin Gruenbaum May 24 '13 at 13:19
  • 1
    That will also stop the propagation of that event. – alex May 24 '13 at 13:21
  • Yep, it works, http://jsfiddle.net/9VgR3/ . Also, the other one doesn't http://jsfiddle.net/u9hmU/ – Benjamin Gruenbaum May 24 '13 at 13:21
  • 1
    Please see also [event.preventDefault() vs. return false](http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false) In addition, `return false` only works from withing jQuery callbacks, whereas [`preventDefault` is the appropriate DOM method](https://developer.mozilla.org/en-US/docs/Web/API/event.preventDefault) – feeela May 24 '13 at 13:21
  • @feeela And that's intentional. I really intend to stop the event in all aspects, both from firing on the image itself and from bubbling to other elements of the DOM. – Geeky Guy May 24 '13 at 13:23
1

$("#id12").on('click',function(e){ e.preventDefault(); });

user1
  • 1,063
  • 1
  • 8
  • 28
1
    $("#id12").on('click',function(e){
       e.preventDefault();
    });
Ales
  • 898
  • 1
  • 7
  • 13
-1

Below is pure javascript code. Try this :

document.getElementById('id12').setAttribute("onclick", null);
Sudarshan Tanwar
  • 3,537
  • 3
  • 24
  • 39
  • This does _not_ do the same thing as the other answers or what OP is asking for. It'll just remove the onclick attribute (in an ugly way btw, you don't need setAttribute for this!), it will not effect other attached event listeners. The purely DOM method would be using addEventListener and calling stopPropegation from within that (shimming attachEvent for IE, and bubble=false also for IE if you want to stop bubbling, and using event.returnValue=false) – Benjamin Gruenbaum May 24 '13 at 13:26