They are most often used to uniquely identify elements for styling (CSS) and scripts (JavaScript, etc.).
But if you ask about HTML and only HTML, then one example where declarative identifiers are useful is associated with <label> using the <input> , <button> or <textarea> control through its for attribute:
<label for="ex">Example field:</label> <input type="text" name="ex" id="ex">
Without assigning this attribute, activating the label does nothing, but when you connect both elements together with for and id , activating the label causes its control to get focus.
Another way to associate a form label with its control is to keep it inside the label:
<label> Example field: <input type="text" name="ex"> </label>
But this does not always correspond to the structure of the form or page, therefore, as an alternative, a link to the identifier is offered.
Other circumstances in which the id attribute serves as a function are widely discussed in Alocci's answer .
Boltclock
source share