118

I have created a checkbox dynamically. I have used addEventListener to call a function on click of the checkbox, which works in Google Chrome and Firefox but doesn't work in Internet Explorer 8. This is my code:

var _checkbox = document.createElement("input");
_checkbox.addEventListener("click", setCheckedValues, false);

setCheckedValues is my event handler.

ravi404
  • 7,119
  • 4
  • 31
  • 40

9 Answers9

215

Try:

if (_checkbox.addEventListener) {
    _checkbox.addEventListener("click", setCheckedValues, false);
}
else {
    _checkbox.attachEvent("onclick", setCheckedValues);
}

Update:: For Internet Explorer versions prior to IE9, attachEvent method should be used to register the specified listener to the EventTarget it is called on, for others addEventListener should be used.

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
44

You have to use attachEvent in IE versions prior to IE9. Detect whether addEventListener is defined and use attachEvent if it isn't:

if(_checkbox.addEventListener)
    _checkbox.addEventListener("click",setCheckedValues,false);
else
    _checkbox.attachEvent("onclick",setCheckedValues);
//                         ^^ -- onclick, not click

Note that IE11 will remove attachEvent.

See also:

groovecoder
  • 1,551
  • 1
  • 17
  • 27
Zeta
  • 103,620
  • 13
  • 194
  • 236
13

This is also simple crossbrowser solution:

var addEvent =  window.attachEvent||window.addEventListener;
var event = window.attachEvent ? 'onclick' : 'click';
addEvent(event, function(){
    alert('Hello!')
});

Instead of 'click' can be any event of course.

Sergey Onishchenko
  • 6,943
  • 4
  • 44
  • 51
  • +1, I think this is a smart workaround! `addEventListener`'s third argument is optional anyway, so this can be a good solution, and it's nicer than the if-else branches. But in this case, `_checkbox` is the target element, not `window`. :) So maybe you could create a function where the event target is another argument. – Sk8erPeter May 18 '13 at 23:09
  • Oh, I upvoted too early... :) I get a _"TypeError: Illegal invocation"_ in Chrome if I try to alias event target elements' `addEventListener` calls - see this topic: http://stackoverflow.com/questions/1007340/javascript-function-aliasing-doesnt-seem-to-work. So for the `window` object, it works correctly, but does not work for other nodes inside the document. This way your suggested aliasing is NOT correct for the case mentioned in the original question! What would be your workaround? – Sk8erPeter May 18 '13 at 23:59
4

You can use the below addEvent() function to add events for most things but note that for XMLHttpRequest if (el.attachEvent) will fail in IE8, because it doesn't support XMLHttpRequest.attachEvent() so you have to use XMLHttpRequest.onload = function() {} instead.

function addEvent(el, e, f) {
    if (el.attachEvent) {
        return el.attachEvent('on'+e, f);
    }
    else {
        return el.addEventListener(e, f, false);
    }
}

var ajax = new XMLHttpRequest();
ajax.onload = function(e) {
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Anon
  • 41
  • 1
3

IE doesn't support addEventListener until version 9, so you have to use attachEvent, here's an example:

if (!someElement.addEventListener) {
    _checkbox.attachEvent("onclick", setCheckedValues);
}
else {
    _checkbox.addEventListener("click", setCheckedValues, false);
}
ticky
  • 107
  • 8
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
2

I've opted for a quick Polyfill based on the above answers:

//# Polyfill
window.addEventListener = window.addEventListener || function (e, f) { window.attachEvent('on' + e, f); };

//# Standard usage
window.addEventListener("message", function(){ /*...*/ }, false);

Of course, like the answers above this doesn't ensure that window.attachEvent exists, which may or may not be an issue.

Campbeln
  • 2,880
  • 3
  • 33
  • 33
2

Mayb it's easier (and has more performance) if you delegate the event handling to another element, for example your table

$('idOfYourTable').on("click", "input:checkbox", function(){

});

in this way you will have only one event handler, and this will work also for newly added elements. This requires jQuery >= 1.7

Otherwise use delegate()

$('idOfYourTable').delegate("input:checkbox", "click", function(){

});
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
0
if (document.addEventListener) {
    document.addEventListener("click", attachEvent, false);
}
else {
    document.attachEvent("onclick", attachEvent);
}
function attachEvent(ev) {
    var target = ev.target || ev.srcElement;
    // custom code
}
Pramod Kumar
  • 115
  • 1
  • 4
0

If you use jQuery you can write:

$( _checkbox ).click( function( e ){ /*process event here*/ } )
philipp
  • 15,947
  • 15
  • 61
  • 106