Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the For attribute for in an HTML tag?

Tags:

html

  <label id="label1" for="txtTextBox">

what is the impact of label1 if I put the for attribute in there?

like image 250
Diskdrive Avatar asked Sep 10 '10 00:09

Diskdrive


1 Answers

It allows you to create a label that is attached to a specific element in the DOM. When the label receives a mouse down event it focuses the element it is attached to.

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

When the user clicks on the "Username:" text it will focus the text box.

This is also good for accessibility as screen readers will read the label's inner HTML before reading the input field, regardless of the physical order they appear in the DOM.

like image 58
Michael Shimmins Avatar answered Oct 12 '22 12:10

Michael Shimmins