You are here: Reference > JavaScript > client-side > event handling > properties > event (script)

event property (script)

Browser support:
Sets or retrieves an event that the script element should listen for.
Use it together with the htmlFor property that specifies the source object of the event. For a cross-browser solution use inline event attributes. See Example 2 for details.
If you want to add event listeners dynamically, use the addEventListener or attachEvent methods.

Syntax:

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

Possible values:

String that sets or retrieves the name of the event. For a complete list of events in HTML, see the page for events.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the event 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 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

Example HTML code 3:

This example shows how to change the registered event type to another:
<head>
    <script type="text/javascript" id="myScript" for="Button" event="onclick">
        alert ("You have clicked on the button.");
    </script>
    <script type="text/javascript">
        function ChangeEventType () {
            var script = document.getElementById ("myScript");
            script.event = "onmouseover";
        }
    </script>
</head>
<body>
    <button id="Button">Sample button</button>

    <ol>
        <li>Click on the Sample button first to check whether the alert box is opened,</li>
        <li>Click on the 'Change to onmouseover' button,</li>
        <li>Just move the mouse pointer over the 'Sample button'.</li>
    </ol>
    <button onclick="ChangeEventType ();">Change to onmouseover!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content