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?
Asked
Active
Viewed 1,019 times
2

KhAn SaAb
- 5,248
- 5
- 31
- 52
-
You can try this, `onclick="return false;"` – Satpal May 24 '13 at 13:19
-
`unbind` should work for any events; additionally try `off` for newer Query versions – feeela May 24 '13 at 13:19
-
are i have div on click of Div inside click function i want to disable image click event – KhAn SaAb May 24 '13 at 13:21
6 Answers
7
$("#id12").on('click',function(e){
e.preventDefault();
});

Claudio Redi
- 67,454
- 15
- 130
- 155

PSR
- 39,804
- 41
- 111
- 151
-
-
-
@Claudio Redi question is to disabled click for image ... indive div on click function – anam May 24 '13 at 13:47
1
$("#id12").click(function () { return false; });

Geeky Guy
- 9,229
- 4
- 42
- 62
-
2Actually, if I remember correctly this works, why was this downvoted? – Benjamin Gruenbaum May 24 '13 at 13:19
-
1
-
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
-
1Please 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
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