require-input-label
This rule enforces the presence of accessible labels for input elements such as <input type="text">, <select> and <textarea>. Recommended to have them adjacent to the element itself, but the rule will try to find one in the whole available DOM tree. Some exclusions apply:
inputelements of typebutton,reset,submitare excluded, if they have a non-empty and non-whitespacevalueattribute.inputelements of typeimageare excluded, if they have at least one ofalt,titleorvalueattribute, which is non-empty and non-whitespace.inputelements of typehiddenare excluded, since there are not rendered.- Elements with
aria-labelledbyoraria-labelare excluded.
Why?
Labels improve accessibility for users of assistive technologies by ensuring that form controls are properly described.
How to use
module.exports = {
rules: {
"@html-eslint/require-input-label": "error",
},
};
Rule Details
Examples of incorrect code for this rule:
<input type="text">
<textarea></textarea>
<select></select>
<input type="button">
<input type="button" value" ">
<input type="image">
Examples of correct code for this rule:
<label for="name">Name:</label><input id="name">
<label>name: <input></label>
<textarea aria-labelledby="foo"></textarea>
<input type="hidden">
<input type="button" value="Button">
<input type="image" alt="Click me">