Prototype Radio Button Serialization Bug
October 8th, 2007It turns out that my favorite JavaScript library has a rather weird chink in its otherwise impregnable armor. The Form.Serialize method makes a complete hash (no not an object hash, a right royal mess kind of hash) of serializing radio buttons. Google of course came to the rescue with this very excellent fix from Rex Chung.
This works great if you’ve got one set of radio buttons, I had an unknown number to deal with. So here is a slightly modified version that uses Prototype’s findAll:
Form.getInputs('formName','radio').findAll(function(rad){ return rad.checked; }).each(function(rad) {
//Deal with each radio button here by referencing the method argument "rad"
alert(rad.value);
});
Works like a charm.
