Snippets code from my daily experience

May 30, 2008

Annoying RadioStateChange usage

Filed under: radiogroup,RadioStateChange,xul — dafi @ 8:09 pm

radiogroup is the worst XUL widget I’ve ever used, its usage is made boring due to RadioStateChange listener but the real annoying behavior comes on when you want to change radio selection from code.

It is very innatural because changing radiogroup’s selectionIndex doesn’t trigger RadioStateChange and you need to create an event by hand and trigger it.

var myEvent = document.createEvent(“Events”);
myEvent.initEvent(“RadioStateChange”, true, true);
document.getElementById(“radioGroup”).dispatchEvent(myEvent);

But this is not sufficient, the RadioStateChange handler is called twice, the first one to “unselect” and second one to “select”.

Doesn’t exist a simple manner to determine if handler is called to “unselect” or “select” so you can’t use the radiogroup’s selected propery.

The solution consists to test all radios selected property to find the checked element.

if (document.getElementById(“folders-radio”).selected) {

} else if (document.getElementById(“files-radio”).selected) {

}

Blog at WordPress.com.