HTMLHint icon indicating copy to clipboard operation
HTMLHint copied to clipboard

`input-requires-label` rule should allow input tag nested inside label tag

Open at6ue opened this issue 3 years ago • 2 comments

Currently, the input-requires-label rule forces the input tag to have an id attribute to associate a label tag, but the input tag without id attribute can be considered to be associated with a nesting label tag.

Alternatively, you can nest the <input> directly inside the <label>, in which case the for and id attributes are not needed because the association is implicit: <label>: The Input Label element - HTML: HyperText Markup Language | MDN

According to HTML Standard,

If the for attribute is not specified, but the label element has a labelable element descendant, then the first such descendant in tree order is the label element's labeled control.

So, my idea is to modify the rule to allow the first input tag nested inside a label tag without the for attribute.

Here is a small example: clicking on the "bar" label toggles the "baz" checkbox associated with the for attribute, instead of the nested "bar" checkbox. https://jsfiddle.net/pL5s0mvk/

at6ue avatar Sep 19 '22 08:09 at6ue

Yes, the wrapping is valid, so you don't need to turn on this rule if you're using that style. All rules are opt-in

nschonni avatar Sep 19 '22 14:09 nschonni

Unfortunately, our codebase has both styles, so for the moment we ignore warnings against nested input tags.

Is the rule open for modification on that point? If yes, I or someone else can consider sending a PR.

at6ue avatar Sep 23 '22 13:09 at6ue