Definition and usage
The For property specifies which form element the label binds to.
Implicit and explicit contact
Tags are typically associated with form controls in one of the following two ways: the form control as the content of the tag label, either implicitly, or by naming a target form ID for the for property under the <label> tag, which is an explicit form.
For example, in XHTML:
Explicit Contact:
[HTML] view Plaincopy
- <label for= "SSN" >social Security number:</label>
- <input type= "text" name= "Socsecnum" id= "SSn"/>
Implicit contact:
[HTML] view Plaincopy
- <label>date of Birth: <input type= "text" name= "DOFB"/></label>
The first tag is to associate the text "Social Security Number:" With the text input control ("Socsecnum") of the form's Social Security numbers in an explicit form, with the value of the for property being the same as the ID of the control, which is SSN. The second tag ("Date of Birth:") does not require a for property, and its related controls do not require an id attribute, which is implicitly connected by placing the <input> tag in the <label> tag.
Instance
A simple HTML form with two input fields and related tags:
[HTML] view Plaincopy
- <form>
- <label for= "Male" >Male</label>
- <input type= "Radio" name= "Sex" id= "male"/>
- <br/>
- <label for= "female" >Female</label>
- <input type= "Radio" name= "Sex" id= "female"/>
- </form>
Also: Make connections in JavaScript
*-------Add an ID for each radio tag, add a for property for each label label-------window.onload = function () {var thewrapdiv = Document.getelementby Id (' Carlist '); var thelabel = thewrapdiv.getelementsbytagname ("label"); var theinput = thewrapdiv.getelementsbytagname ("input"), for (i = 0; i < theinput.length; i + +) {theinput[i].id = "sta TE "+ i; }for (j = 0; J < Thelabel.length; J + +) {Thelabel[j].setattribute ("for", "state" + j); } }
The For property of the html<label> tag