Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have radio button's label make selection too?

According to (somewhat official) this guide, I can make a radio button's label make the selection for that radio button too. They say,

Always use labels for each checkbox and radio button. They associate text with a specific option and provide a larger clickable region.

Unfortunately, I have not been able to get this functionality for my form. Here is my code:

<% form_for(@bet) do |f| %>
  <%= f.label :bet_type %>
  <%= f.radio_button :bet_type, "moneyline" %>
  <%= f.label :bet_type_moneyline, "Moneyline" %>
  <%= f.radio_button :bet_type, "spread" %>
  <%= f.label :bet_type_spread, "Spread" %>
<% end %>

I've also tried this (because this is the example's way using FormTagHelper instead of FormHelper):

<% form_for(@bet) do |f| %>
  <%= radio_button_tag :bet_type, "moneyline" %>
  <%= label_tag :bet_type_moneyline, "Moneyline" %>
  <%= radio_button_tag :bet_type, "spread" %>
  <%= label_tag :bet_type_spread, "Spread" %>
<% end %>

But, I still cannot get it to work. I'm using rails 2.3.5 and ruby 1.8.7.

Thanks for reading, and maybe helping!

like image 542
t-mart Avatar asked Jan 09 '10 07:01

t-mart


People also ask

How do I select a radio button when a label is clicked?

To select a radio button by clicking on its text in React:Add a label element for each radio button. The htmlFor prop of each label should be set to the id of each radio button. Click on the label element to select the radio button.

How do you put a label inside a radio button?

To label a radio button, add a <label> element after the <input> element and insert a for attribute with the same value as the id of the associated <input> element. Then, write your label text in the <label> tag.

How do I make the radio button selected by default?

You can check a radio button by default by adding the checked HTML attribute to the <input> element. You can disable a radio button by adding the disabled HTML attribute to both the <label> and the <input> .

Does onClick work with radio buttons?

Responding to Click EventsWhen the user selects one of the radio buttons, the corresponding RadioButton object receives an on-click event. To define the click event handler for a button, add the android:onClick attribute to the <RadioButton> element in your XML layout.


1 Answers

You may found a solution but it can help others. I'm using Rails 4+.

You can make the label clickable properly using Rails' FormHelper's label method using the "value" key into params.

label(:post, :privacy, "Public Post", value: "public")
# => <label for="post_privacy_public">Public Post</label>

For your code, it should be something like

<%= f.radio_button :bet_type, "moneyline" %>
<%= f.label :bet_type, "Moneyline", value: "moneyline" %>
<%= f.radio_button :bet_type, "spread" %>
<%= f.label :bet_type, "Spread", value: "spread" %>

Hope it helps :)

like image 106
user3309558 Avatar answered Oct 12 '22 12:10

user3309558