Posts Tagged ‘javascript’

Internet Explorer and Ajax

Tuesday, June 17th, 2008

Okay, I just wasted waaaay too much time trying to track down an apparently odd Internet Explorer redraw issue. The problem was an ajax call associated with the onchange of a checkbox form element. In Firefox this worked perfectly. Click the button, the div updates itself. However, in IE 6 and 7 it would just sit there and do nothing until you would force a redraw (ctrl +a seemed to do it), or scrolled the page up & down a few times.

It turns out, this was because of the way IE handles the onchange javascript event. This only fires once the checkbox loses focus. To fix this, I just switched from the onchange event to the onclick event and everything works as expected. I’ll retract some (okay, just one) of my nasty comments about Internet Explorer now as this does make logical sense thinking about it now.

So the offending code looked like

<input type="checkbox" name="mycheckbox" value="something" onchange="update_div();">click me

whereas it should be:

<input type="checkbox" name="mycheckbox" value="something" onclick="update_div();">click me