I have a div as a trigger for some jQuery. The div contains a link. Sometimes clicking the div will also mean clicking the link and I need to stop the link’s behaviour.
I believe this can be done with event.preventDefault(); but the following doesn’t work in IE8. Im assuming this is because the trigger is a parent div not the actual link, but how do I fix this? Thanks
$('div#myDiv').click(function(){
event.preventDefault();
//other js here
});
<div id="myDiv">
<a href="#">Link</a>
</div>
You didn’t define the parameter:
.preventDefaultis a jQuery shim in the jQuery event object. It doesn’t exist in the IE event object.Chrome and IE define a global
eventobject which refers to the native event object in those browsers. For this reason, always useeevevtetc so you don’t accidentally refer to this global event object as it won’t get the jQuery bug fixes and normalizations.Documentation: http://api.jquery.com/category/events/event-object/