jeffreynolte
jeffreynolte

Reputation: 3779

Do I really have to remove a second, hidden form field from within a label for my HTML to validate?

We have the following code in which we are getting errors in the w3c validator for "Any input descendant of a label element with a for attribute must have an ID value that matches that for attribute." and "The label element may contain at most one input, button, select, textarea, or keygen descendant." Is this something that should just be ignored by the validator (as it is seeminlgly correct) or should it be changed to appease the w3c? Note this is html5 doctype.

<fieldset>
  <label for="user_is_subscribed">
    <input type="hidden" value="0" name="user[is_subscribed]">
    <input type="checkbox" value="1" name="user[is_subscribed]" id="user_is_subscribed">
    Newsletter Signup
  </label>
  <span class="checkLabel">We will never spam or give away your information</span>
</fieldset>

Thank in advance!

Upvotes: 3

Views: 1755

Answers (2)

Quentin
Quentin

Reputation: 944008

Is this something that should just be ignored by the validator

No

(as it is seeminlgly correct)

It isn't

or should it be changed

Yes

to appease the w3c?

No. It should be changed because it is wrong, and browsers have to error correct to figure out which element the label is associated with.

The label isn't labeling the hidden input, move it elsewhere.

Upvotes: 7

Matt Ball
Matt Ball

Reputation: 359966

Labels should contain at most one input element. Move the hidden input out of the label. Also, when an input is a descendant of a label, the for attribute is superfluous.

<fieldset>
  <input type="hidden" value="0" name="user[is_subscribed]">
  <label>
    <input type="checkbox" value="1" name="user[is_subscribed]" id="user_is_subscribed">
    Newsletter Signup
  </label>
  <span class="checkLabel">We will never spam or give away your information</span>
</fieldset>

Upvotes: 8

Related Questions