You are here: Reference > JavaScript > client-side > HTML DOM > properties > htmlFor (label)

htmlFor property (label)

Browser support:
Sets or retrieves the identifier of the object to which the label element is attached. The label element can be used to associate information with controls.

Syntax:

object.htmlFor;
You can find the related objects in the Supported by objects section below.
This property is read/write.
HTML page for this property: for

Possible values:

String that sets or retrieves the identifier of the associated object.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the for attribute:
<form>
    <input type="checkbox" id="chBox" />
    <label for="chBox">You can change the checkbox state by clicking on this text</label>
</form>
Did you find this example helpful? yes no

Example HTML code 2:

This example illustrates the use of the htmlFor property:
<head>
    <script type="text/javascript">
        function GetHtmlFor () {        
            var table = document.getElementById ("myTable");
            var labels = table.getElementsByTagName ("label");
            
            for (var i = 0; i < labels.length; i++) {
                alert (labels[i].htmlFor);
            }
        }
    </script>
</head>
<body>
    <form>
        <table id="myTable">
            <tr>
                <td><label for="fname">First Name</label>
                <td><input type="text" name="forename" id="fname" />
            </tr>
            <tr>
                <td><label for="sname">Last Name</label>
                <td><input type="text" name="surname" id="sname" />
            </tr>
        </table>
    </form>


    <br />
    <button onclick="GetHtmlFor ();">Get for attributes with htmlFor properties!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

External links:

User Contributed Comments

Post Content

Post Content