Search code examples
javascripthtmljqueryinternet-explorer-9jquery-events

`onclick` event not called when element clicked with Internet Explorer 9


This solution does not work for IE9.

I have the following code:

$(document).ready(function() {
      document.getElementById('unsubref').onclick = function() {unsubcribe();}; 
});

and unsubref is:

<div id="unsub">
<h1>Click <a id="unsubref">here</a> to unsubscribe from the mailing service</h1>
</div>

in Chrome, it works just fine. The function unsubscribe is being invoked with alert.

However, in IE9 it does not work!


Solution

  • Since you are using jQuery I would recommend you using the .click() method to subscribe to the handler:

    $(document).ready(function() {
        $('#unsubref').click(unsubcribe); 
    });
    

    or using an anonymous function:

    $(document).ready(function() {
        $('#unsubref').click(function() {
            alert('unsubscribing ...');
        }); 
    });