jquery - Why unbind event on image is not working -
i have got image on want disable click event , have used unbind event shown below
$('#myimage').unbind("click");
but please let me why click event still working ??
this code
$(document).ready(function () { $('#myimage').unbind("click"); }) $(document).on('click', ' #myimage', function (event) { alert('i called'); });
this fiddle
the issue in fiddle code block running in document.ready
handler (due settings you've chosen) unbind()
called before event bound, hence no event removed.
to fix actual problem, need use delegated form of off()
method attached delegated event handler using on()
, this:
<script> $(document).ready(function() { $(document).off('click', '#myimage'); }) $(document).on('click', '#myimage', function(event) { alert('i called'); }); </script>
note in settings of fiddle js code set execute 'no wrap - in <head>
'
Comments
Post a Comment