Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to associate label elements with radio buttons [duplicate]

Tags:

I have some option buttons in my form and I always need to click exactly on the round option button to select it. Is there a way to allow user to click on the associated label to select the option button?

enter image description here

<p>
    <span class="editor-label">
        @Html.LabelFor(m => m.ADR)
    </span>
    <span class="editor-field">
        @Html.RadioButtonFor(model => model.ADR, "Yes", AdrYesOptions) 
        @UserResource.YesValue
        @Html.RadioButtonFor(model => model.ADR, "No", AdrNoOptions) 
        @UserResource.NoValue 

    </span>
</p>

I found some solutions here: How to associate labels with radio buttons but these don't suit me because I prefer a solution specific for MVC.

like image 450
Bronzato Avatar asked Mar 03 '12 11:03

Bronzato


People also ask

How do you associate a label with 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 you keep a radio button and label on the same line?

As a quick solution either you can apply colspan for your td or You can have both the radio button controls in same td so that the change due to long text wont affect the display of radiobutton.

How do radio buttons in HTML associated with each other?

HTML Radio button is typically used to select a particular option from a group of related options. To define a radio button, we use the <input> element of HTML. When a particular option is selected using a radio button, the other options are deselected i.e., one can only select a single option at a time.

Can multiple radio buttons have same name?

Radio buttons allow only one choice within a group of buttons. Each radio button within a group should have the same name. You can create more than one group of radio buttons by using different names.


1 Answers

Wrap each radio button and associated label text inside a <label> element:

<p>
    <span class="editor-label"> @Html.LabelFor(m => m.ADR) </span>
    <span class="editor-field">
        <label> @Html.RadioButtonFor(model => model.ADR, "Yes", AdrYesOptions) @UserResource.YesValue </label>
        <label> @Html.RadioButtonFor(model => model.ADR, "No", AdrNoOptions) @UserResource.NoValue </label>
    </span>   
</p> 
like image 123
Michael Liu Avatar answered Oct 11 '22 02:10

Michael Liu