Thursday, September 5, 2013

onmousemove event handling in Internet Explorer

IE was giving me trouble when assigning event handlers in my usual syntax:
document.getElementById("myDiv").onmousemove = function (e) {
    // code to execute
}

The general consensus around the internet is that IE doesn't bind onmousemove events to elements other than the 'document' element. Or something like that. We all know that IE sucks, so lets cut straight the workaround.

It seems it works much better when using addEventListener():
document.getElementById("myDiv").addEventListener("mousemove", function (e) {
    // code to execute
}

Note that in that syntax the event goes by the name 'mousemove' and not 'ONmousemove'

No comments:

Post a Comment