I have the following HTML:
<img src='img.png' onclick='document.write("Hi there!")' />
...and the following JavaScript:
$(document).ready(function() {
// Change the click handler
$('img').click(function() {
alert("Hi there!");
});
});
What I am expecting to happen is that the click handler for the image is replaced and that clicking it will produce an alert dialog. Instead, the page content changes, as dictated by the click handler in the tag.
How can I change the click handler at runtime?
Note: Here is an interactive demo you can play with:
http://jsfiddle.net/ykmaG/
You need to remove the original onclick
:
$('img').removeAttr('onclick').click(function() { ... });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With