Explain, in order not only to memorize but also to understand why you need a label tag for select, etc.?
2 answers
If we are talking about the tag , then:
The
<label>establishes a link between a specific label, which is usually text, and a form element (<input>,<select>,<textarea>). Such a relationship is necessary to change the values of form elements when you click on the text with the mouse cursor. In addition, using<label>you can set hotkeys on the keyboard and go to the active item like links.There are two ways to associate an object and a label. The first is to use the id
idinside the form element and specify its name as theforattribute of the<label>. In the second method, the form element is placed inside the<label>container.
<input type="checkbox" id="check1"><label for="check1">Lorem</label> <input type="checkbox" id="check2"><label for="check2">Ipsum</label> <input type="checkbox" id="check3"><label for="check3">Dolor</label> <input type="checkbox" id="check4"><label for="check4">Sit amet</label> If it’s all about the attribute , then:
The attribute is intended to indicate the label of an item in the list, abbreviated as compared with the text inside the
<option>. If thelabelattribute is present, the text inside the<option>ignored and thelabelvalue is displayed in the list.
<select name="question"> <option label="Бах" value="1">Иоганн Себастьян Бах</option> <option label="Бетховен" value="2">Людвиг ван Бетховен</option> <option label="Шопен" value="3">Фредерик Шопен</option> <option label="Огинский" value="4">Михаил Клеофас Огинский</option> </select> It is difficult to explain. But for example, you have a radio button.
- Here the text and the point is the radio baton itself.
So if you click on that text under the label, it will also turn on the radio button. It sounds stupid, but this is his only use that I have found.
<label>element is passed to the element withidspecified in theforattribute. - Igor