HTML HTML Forms Choosing Options Radio Buttons

Vittorio Somaschini
Vittorio Somaschini
33,367 Points

Where to put the label

Hello.

I would like to ask this: I am doing the radio button challenge in html and with particular focus on the label.

With this code I do not pass the challenge:

<label>Shirt Size:</label>
      <label for="small">Small</label>
      <input type="radio" id="small" value="small" name="shirt_size">
      <label for="medium">Medium</label>
      <input type="radio" id="medium" value="medium" name="shirt_size">
      <label for="large">Large</label>
      <input type="radio" id="large" value="large" name="shirt_size">

I get this error: Oops! It looks like Task 3 is no longer passing.

I went back to challenge 3/4 and the error I got was the Label must not be associated to any button.

With this code I pass it:

 <label>Shirt Size:</label>
      <input type="radio" id="small" value="small" name="shirt_size">
      <label for="small">Small</label>
      <input type="radio" id="medium" value="medium" name="shirt_size">
      <label for="small">Medium</label>
      <input type="radio" id="large" value="large" name="shirt_size">
      <label for="small">Large</label>

Is this something just relating to the treehouse environment or would my first code not work only in "real" website development?

I ask this because I can't understand why the position matters, since each single label has its for value assigned.

Ty

Vittorio

4 Answers

Mike Francis
Mike Francis
3,550 Points

I think it's because you're writing a label for an id that you have not yet defined. You are only defining the id in the follow code when you write the <input> code.

ie:

<label for="small">Small</label>
<input type="radio" id="small" value="small" name="shirt_size">

vs

<input type="radio" id="small" value="small" name="shirt_size">
<label for="small">Small</label>

In the second code, you have defined the id="small" that you then attach the label to. In the first code example, you haven't done that, so the id="small" doesn't yet exist when you are trying to attach a label to it.

Hope that helps.

Khaleel Hamid
Khaleel Hamid
5,258 Points

You have to put each label right beside the input after the name="">

Like this.

      <label>Shirt Size:</label>
      <input type="radio" id="small" value="small" name="shirt_size"><label for="small" class="light">Small</label><br>
      <input type="radio" id="medium" value="medium" name="shirt_size"><label for="medium" class="light">Medium</label><br>
      <input type="radio" id="large" value="large" name="shirt_size"><label for="large" class="light">Large</label>

I don't think you need class="light" tho you might just remember that from Nick's lesson.

Vittorio Somaschini
Vittorio Somaschini
33,367 Points

Mmmm I do not think that is the case..

I think that if I put the label first it should just place the label before the button, which may be ugly but should work fine.

I have tried to do the same in the following code challenge and it worked (that new code challenge does not have the main label on top) so I have presumed it must be something related to the environment I coded my first example in.

But maybe more trustable voices (than mine) will write something.

Ty ;)

Robert Mews
Robert Mews
11,540 Points

I'm with you on this one. Previously when we created input fields for our form the label element went before the input element. However, this challenge was the opposite, where the label was after the input. I placed my labels before the inputs and the code displayed fine. Not sure what a good answer is, unfortunately...

I was having the same problem as Vittorio and Khaleel's answer made me pass (finally!). I didn't realize that a line of code would ever have to be on the same line as another piece of code. I thought spacing didn't really matter, but I'm a beginner.

Thanks, Khaleel!

Khaleel Hamid
Khaleel Hamid
5,258 Points

Glad it helped you :)

Sometimes it can be smallest thing from preventing your code to work, you'll learn that as you go along lol :P

Good luck.