The For property of the html<label> tag

Source: Internet
Author: User

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

    1. <label for= "SSN" >social Security number:</label>
    2. <input type= "text" name= "Socsecnum" id= "SSn"/>

Implicit contact:

[HTML] view Plaincopy

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

    1. <form>
    2. <label for= "Male" >Male</label>
    3. <input type= "Radio" name= "Sex" id= "male"/>
    4. <br/>
    5. <label for= "female" >Female</label>
    6. <input type= "Radio" name= "Sex" id= "female"/>
    7. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.