Attaching event workaround for IE, checking radio inputs unreliable (bug)

Posted Wednesday, July 26, 2006

Filed under: , , ,

That old piece of crap IE has bugged me once again. First it couldn't attach an event, and second it wouldn't tell my javascript which radio input was checked or not. End result : I use an unrecognised (but standard) method to attach my script so IE users don't get a bloated html.

Onchange, onkeypress, onfocus etc are a bit different from regular attributes : their value is an event handler which has to be registered by the browser. Here is what worked for me in FF/IE/OP:
elements[i].onchange = function() {toggle()}
But since my event handler was abused beyond repair by IE's misbehaviour, I rely on the following to attach my handler in other browsers:
elements[i].setAttribute("onchange", "toggle()")

The reason I had to hide my script from IE is that it couldn't figure out how many radio inputs were checked. Contrary to FF and OP, the following test proved true when it shoudln't, and false in other cases:
if (radio1.checked == true && radio2.checked == true)
I relied on it to show/hide an input field but when it wrongly displayed it, clicking inside the field hid it back in IE.
This probably comes from the "onchange" event being fired too early (namely before the change) I guess.
Not sure I made myself clear but I resolved to leaving IE users with plain ol' html.

Comments disabled because of spammers.

comment #1 On 07/03, Rob wrote :

I hear your pain...

There is also a discussion of the issue with the onchange event on radio buttons at...

The summary there was to use onclick rather than onchange for radio buttons. (or alternatively adding a onclick="this.blur();" ).

comment #2 On 22/03, Jasper wrote :

This is a known bug with IE. OnChange doesn't actually fire for radio buttons until you effectively ".blur()" the button.

Workaround discussed here:

Also of note, be careful with checkboxes! they can cause a major lockup in IE (6,7, and 8) if the user isn't careful.

Nuts! I know, but IE is just soooooo broken!

comment #3 On 15/07, pipsoed wrote :

Thanks a lot for onClick!

Technorati Profile