Have radio button's label make selection too?

html, ruby, ruby-on-rails, web-applications

Solution

An easy way to do this for radio buttons is to place the input tag inside the label, like so:

<label>
  <input />
  This is an input!
</label>

This is a valid way of accomplishing your goal.

In Rails the `label` helper can accept a block, so you could do:

<%= f.label :moneyline do %>
  <%= f.radio_button :bet_type, "moneyline" %>
  "Moneyline"
<% end %>

Problem

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!

Original source