What is the HTML for=”” attribute in ?

The for attribute is used in labels. It refers to the id of the element this label is associated with.

For example:

<label for="username">Username</label>
<input type="text" id="username" name="username" />

Now when the user clicks with the mouse on the username text the browser will automatically put the focus in the corresponding input field. This also works with other input elements such as <textbox> and <select>.

Quote from the specification:

This attribute explicitly associates the label being defined with
another control. When present, the value of this attribute must be the
same as the value of the id attribute of some other control in the
same document. When absent, the label being defined is associated with
the element’s contents.

As far as why your question is tagged with jQuery and where did you see it being used in jQuery I cannot answer because you didn’t provide much information.

Maybe it was used in a jQuery selector to find the corresponding input element given a label instance:

var label = $('label');
label.each(function() {
    // get the corresponding input element of the label:
    var input = $('#' + $(this).attr('for'));
});

Leave a Comment