How to make label for radio button

1.5k views Asked by At

I have some radio buttons. But I also want to the text clickable. So that the ratio button is selected. So not only if you click on the radio button that the ratio buttion is selected, but also if you click on the text, that the ratio button is selected.

I have this:

<td>
  <input id="upload" name="folder" type="radio" value="@item" />
  <label>@Html.Label(item)</label>
</td>

Thank you

I try it like this:

<div class="form-group">
   <div class="col-md-offset-2 col-md-10">
        <table>
            @foreach (var item in Model.Directories)
            {
                <tr>
                    <td>
                        <input id="upload" name="folder" type="radio" value="@item" />
                        <label for="upload">@Html.Label(item)</label>
                    </td>
                </tr>
             }
        </table>
    </div>
</div>

I have it now like this:

 <div class="form-group">
                                <div class="col-md-offset-2 col-md-10">
                                    <table>
                                        @foreach (var item in Model.Directories)
                {
                                            <tr>
                                                <td>
                                                    <label>
                                                        <input type="radio" name="folder" value="@item" id="upload">
                                                        <label for="folder">@Html.Label(item)</label>
                                                    </label>
                                                </td>
                                            </tr>
                }
                                    </table>
                                </div>
                            </div>

but the radio buttons are in a foreach loop, so the id is different of every radio button

I try it like this:

  <div class="form-group">
                                <div class="col-md-offset-2 col-md-10">
                                    <table>
                                        @foreach (var item in Model.Directories)
                {
                                          @for(var i=0; i < item.Count; i++){
                                            <tr>
                                                <td>


                                                        <fieldset>
                                                            <input type="radio" name="folder" value="@item" id="folder">
                                                            <label for="folder">@Html.Label(item)</label>
                                                        </fieldset>

                                                </td>
                                            </tr>
                }
                                        }

                                    </table>
                                </div>
                            </div>
5

There are 5 answers

0
Akshay On

use label for

<td>
  <input id="upload" name="folder" type="radio" value="@item" />
  <label for="upload">@Html.Label(item)</label>
</td>

0
potashin On

You can use for attribute with the value of the input's id attribute:

<input id="upload" name="folder" type="radio" value="@item" />
<label for="upload">@Html.Label(item)</label>

0
Alien On

the simple way

    <td>
      <label>
      <input id="upload" name="folder" type="radio" value="@item" />
       @Html.Label(item)</label>
    </td>

1
Praveen Kumar Purushothaman On

Using for attribute:

<label for="folder">@Html.Label(item)</label>
1
Sujendra E On

You need to enclose the radio within a label tag, To display some text add a tag inside the label:

<label for="upload">
  <span>@Html.Label(item)</span>
</label>
<input type="radio" name="folder" value="@item" id="upload" >