Getting Radio buttons value with Prototype.js

In Prototype.js, there’s a handy function $F to get the value of a form element.
However it doesn’t work well with a group radio buttons. The argument of $F is the id of the element, and since id is unique, we can’t use the name of the radio group to get the value, which is essentially how the server side gets the value.


<form name="myform">
<input type="radio" name="type" value="one"/>
<input type="radio" name="type" value="two"/>
<input type="radio" name="type" value="three"/>
</form>

#wrong
var typeValue = $F('type');

Here’s a quick solution.


var typeValue = Form.getInputs('myform','radio','type').find(function(radio) { return radio.checked; }).value;

Advertisement

Hash recursive merge in Ruby

For multi-dimension Hash, .merge will replace the values as a 2 dimension hash.


>> {"number" => {"1" => "one","2" => "two"}}.merge({"number" => {"3" => "three"}})
=> {"number"=>{"3"=>"three"}}

We needed the keep the hash values and merge complex hash together that is loaded from YAML.

Solution?


class Hash
    def recursive_merge(h)
        self.merge!(h) {|key, _old, _new| if _old.class == Hash then _old.recursive_merge(_new) else _new end  }
    end
end

>>{"number" => {"1" => "one","2" => "two"}}.recursive_merge({"number" => {"3" => "three"}})
=> {"number"=>{"1"=>"one", "2"=>"two", "3"=>"three"}}