Reputation: 3077
I want to use a couple of radio buttons to control the mode of some page elements. The basic requirements being that it displays to the user which mode is active and fires the appropriate mode switching function when a different button is selected. So i tried the following.
<%= radio_button_tag(:label_type, "tooltip", :checked, :onchange => 'console.log("tooltip")')%>
<%= label_tag(:label_type_tooltip, "Enable Tooltip") %><br />
<%= radio_button_tag(:label_type, "labels", :onchange => 'console.log("labels")') %>
<%= label_tag(:label_type_labels, "Enable Labels") %>
This seems to work without the :onchange parameter passed to the second radio_button_tag, but with it (as shown above) is has the confusing effect of not attaching an onchange event to the second radio button but rather setting the second radio button to checked (which i dont want) like so:
<input checked="checked" id="label_type_tooltip" name="label_type" onchange="console.log("tooltip")" type="radio" value="tooltip">
<label for="label_type_tooltip">Enable Tooltip</label><br>
<input checked="checked" id="label_type_labels" name="label_type" type="radio" value="labels">
<label for="label_type_labels">Enable Labels</label>
Why? How do i make it behave?
Upvotes: 0
Views: 672
Reputation: 2682
as per the rails documentation here are the arguments of a radio_button_tag
radio_button_tag(name, value, checked = false, options = {})
It seems that you are forgetting to pass the 'checked' value. try the following
<%= radio_button_tag(:label_type, "labels", false, :onchange => 'console.log("labels")') %>
However i would recommend you to add bindings in your js or coffee files instead as this approach isn't considered a good practice. Refer to jquery api for further information. Thanks
Upvotes: 4