How do you make the radio button text to be clickable too?

HtmlRadio Button

Html Problem Overview


i have this radio button (and some other after this one):

<input type="radio" name="group1" value="Milk"> Milk<br>

But if i want to activate the radio button, i have to click it, clicking the word "Milk" which is to the right of the button, doesn't activate it. How can i do that?, all the examples i found about radio buttons so far, have the same issue. Thanks in advance.

Html Solutions


Solution 1 - Html

Here you want to use the label tag.

Something like:

            <label>
                <input type="radio" name="group1" value="Milk">
                Milk
            </label><br/>

Labels tell the browser that everything contained within should be treated as one element (in terms of text. They are not divs)

Take a look at this for an example: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_label

Solution 2 - Html

If you use the label tag, giving your radio button an ID, you should be able to click on the label to select the radio.

<input type="radio" name="group1" value="Milk" id="rad1">
<label for="rad1">Milk</label>

http://jsfiddle.net/tvFgU/1/

This is valid for xhtml 1.0 strict:

enter image description here

Solution 3 - Html

For React JSX

<input type="radio" name="group" value="happy" id="rad1">
<label htmlFor="rad1">YES</label>

<input type="radio" name="group" value="sad" id="rad2">
<label htmlFor="rad2">NO</label>

Here Name attribute would be the same for both radio button.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFaitoView Question on Stackoverflow
Solution 1 - HtmlNathan LaffertyView Answer on Stackoverflow
Solution 2 - HtmlMansfieldView Answer on Stackoverflow
Solution 3 - HtmlAnupam MauryaView Answer on Stackoverflow