# Form helper: Linking radio button to label

**URL:** <https://www.ruby-forum.com/t/form-helper-linking-radio-button-to-label/230227>\
**Category:** Rails\
**Created:** [June 5, 2014, 2:49pm UTC](https://www.ruby-forum.com/t/form-helper-linking-radio-button-to-label/230227 "2014-06-05T14:49:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason\_sam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7993a0/32.png) [@jason\_sam](https://www.ruby-forum.com/u/jason_sam)\
**Post date:** [June 5, 2014, 2:49pm UTC](https://www.ruby-forum.com/t/form-helper-linking-radio-button-to-label/230227/1 "2014-06-05T14:49:19Z")

</div>

My form essentially looks like this:

\<%= form\_for :xxx, url: xxxs\_path, method: ‘get’, enforce\_utf8:true do  
|d| %\>  
\<%= d.text\_field(‘filter’, value: @filter, maxlength:64, size:16) %\>  
\<%= d.radio\_button(:filtertype, ‘regexp’) %\>  
\<%= d.label :filtertype\_regexp, ‘Regular Expression’, value: ‘regexp’  
%\>  
\<%= d.radio\_button(:filtertype, ‘lefteq’, checked:true) %\>  
\<%= d.label :filtertype\_lefteq, ‘Starts with…’, value: ‘lefteq’ %\>  
\<%= d.submit(value=“FILTER LIST BELOW”, name: ‘filter’) %\>  
\<% end %\>

I thought from this association between radio buttons and label,  
clicking on _text_ (i.e. ‘Regular Expression’ should already cause the  
button to be selected, but this is not the case. Did I miss something  
here?

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 5, 2014, 4:25pm UTC](https://www.ruby-forum.com/t/form-helper-linking-radio-button-to-label/230227/2 "2014-06-05T16:25:06Z")

</div>

On Jun 5, 2014, at 8:49 AM, Ronald F. wrote:

> \<%= d.submit(value=“FILTER LIST BELOW”, name: ‘filter’) %\>  
> \<% end %\>
> 
> I thought from this association between radio buttons and label,  
> clicking on _text_ (i.e. ‘Regular Expression’ should already cause the  
> button to be selected, but this is not the case. Did I miss something  
> here?

Look at the generated HTML in a browser. In order for the label to  
affect the radio button, one of two things must be true:

1. Either the label’s “for” attribute exactly matches the radio button’s  
“id” attribute, or
2. The label wraps around the button and does not have a “for”  
attribute.

Sometimes you have to get out and push a little with radio buttons,  
although looking at it briefly, you appear to be doing the right things  
by having the same name input into both the label and the radio.

Walter

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 9, 2014, 3:01pm UTC](https://www.ruby-forum.com/t/form-helper-linking-radio-button-to-label/230227/3 "2014-06-09T15:01:58Z")

</div>

On Jun 9, 2014, at 6:35 AM, Ronald F. wrote:

> > 1. The label wraps around the button and does not have a “for”  
> > attribute.
> 
> Just out of curiosity: How can _that_ be done inside an ERB file?

\<%= f.label :your\_element do %\>  
\<%= f.check\_box :your\_element %\>  
The label text for your element  
\<%- end %\>

That creates a wrapped label. I use this all the time, particularly when  
making a Bootstrap layout.

Walter

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 9, 2014, 12:35pm UTC](https://www.ruby-forum.com/t/form-helper-linking-radio-button-to-label/230227/4 "2014-06-09T12:35:15Z")

</div>

Walter D. wrote in post #1148961:

> Look at the generated HTML in a browser. In order for the label to  
> affect the radio button, one of two things must be true:
> 
> 1. Either the label’s “for” attribute exactly matches the radio button’s  
> “id” attribute, or

Got it! I now can see my mistake. The label should be

\<%= d.label :filtertype\_lefteq, ‘Starts with…’, value: ‘lefteq’ %\>

> 1. The label wraps around the button and does not have a “for”  
> attribute.

Just out of curiosity: How can _that_ be done inside an ERB file?

Ronald
