You are here: Reference > HTML > attributes > for (script)

for attribute (script)

Browser support:
Sets the identifier of the element to which the script is bound when a specific event is fired for the element.
Use it together with the event attribute that specifies the type of the event to listen for. For a cross-browser solution use inline event attributes. See Example 2 for details.
JavaScript page for this attribute: htmlFor. You can find other example(s) there.

Possible values:

String that sets the identifier of an element.
Default: this attribute has no default value.

Example HTML code 1:

This example illustrates the use of the for attribute:
<head>
    <script type="text/javascript" for="myButton" event="onclick">
        alert ("You have clicked on the button.");
    </script>
</head>
<body>
    <button id="myButton">Sample button</button>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example is equivalent to the previous one, but it uses inline event handling to listen for the event:
<head>
    <script type="text/javascript">
        function OnButtonClick () {
            alert ("You have clicked on the button.");
        }
    </script>
</head>
<body>
    <button onclick="OnButtonClick ();">Sample button</button>
</body>
Did you find this example helpful? yes no

Supported by tags:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content