Why do I need label HTML?

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

 2
Author: wiaim, 2016-04-20

2 answers

If it is about tag, then:

The <label> tag establishes a relationship between a specific label, as which is usually the text, and the form element (<input>, <select>, <textarea>). This relationship is necessary to change the values of the form elements when the mouse cursor is clicked on the text. In addition, using <label> you can set keyboard shortcuts and go to the active element is similar to links.

There are two ways linking an object and a placemark. The first is to use the ID id inside the form element and specify it name as the for attribute of the <label> tag. In the second method, the element the form is placed inside the container <label>.

  <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>

Source

If we are talking about theattribute , then:

The

Attribute is intended to indicate a list item label that is shortened compared to the text inside <option>. If the attribute label present, then the text inside the <option> tag is ignored and displayed in the list value label.

 <select name="question">
    <option label="Бах" value="1">Иоганн Себастьян Бах</option>
    <option label="Бетховен" value="2">Людвиг ван Бетховен</option>
    <option label="Шопен" value="3">Фредерик Шопен</option>
    <option label="Огинский" value="4">Михаил Клеофас Огинский</option>
   </select>

Source

 6
Author: lexxl, 2016-04-20 21:14:25

It's hard to explain. But for example, you have a radiobaton.

  • Here is the text and the dot is the radiobaton itself.

So if you click on the text under the label, it will also turn on the radio button. It sounds stupid, but it's the only use I've found for it.

 2
Author: Telion, 2016-04-20 18:47:40