Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to associate a label with a group of radio buttons?

Tags:

html

I was shown that in general, there are two ways of labeling radio buttons:

Method #1:

<label>Male<input type="radio" name="gender" value="m"></label>
<label>Female<input type="radio" name="gender" value="f"></label>

Method #2 (using for):

<label for="id_male">Male</label>
<input type="radio" id="id_male" name="gender" value="m">
<label for="id_female">Female</label>
<input type="radio" id="id_female" name="gender" value="f">

But what if there is a need to associate a group of radio buttons with a label?

i.e:

<label>What is your gender?
    <label>Male<input type="radio" name="gender" value="m"></label>
    <label>Female<input type="radio" name="gender" value="f"></label>
</label>

The questions are:

  1. Is the way used to associate the "What is your gender?" label above correct?

  2. Is there a way to associate "What is your gender?" that corresponds to Method #2 (i.e. using for)?

like image 621
Flux Avatar asked Oct 27 '25 09:10

Flux


1 Answers

use method #2 like this:

<form action="" method="post">
  <fieldset>
    <legend>What's your gender?</legend>
      <label for="id_male">Male</label>
      <input type="radio" id="id_male" name="gender" value="m">
      <label for="id_female">Female</label>
      <input type="radio" id="id_female" name="gender" value="f">
  </fieldset>
</form>

See more about fieldset

like image 162
dippas Avatar answered Oct 30 '25 00:10

dippas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!