Explain, in order not only to memorize but also to understand why you need a label tag for select, etc.?

  • w3schools.com/tags/tag_label.asp - increases the user-friendliness of the interface when using the mouse, since clicking on the text of the <label> element is passed to the element with id specified in the for attribute. - Igor

2 answers 2

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 id inside the form element and specify its name as the for attribute 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> 

A source

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 the label attribute is present, the text inside the <option> ignored and the label value 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> 

A source

    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.