Events and Event Handling - HTML 4

Events are actions that either the browser executes or the user performs while visiting your page. Loading a Web page, moving the mouse over an image, closing a window, and submitting a form are all examples of events.

JavaScript deals with events by using commands called event handlers. Any action by the user on the page triggers an event handler in your script. Table is a list of JavaScript’s event handlers.

Table : Event Handlers

Event HandlersEvent HandlersEvent Handlers
Not all objects support every event handler. For example, the onload handler is supported by only the window and image objects. The onsubmit event handler is supported by only the form object.

A common way to deal with event handlers is to use them as an attribute of an HTML element. This is called an inline event handler. Here is an example of the onsubmit inline event handler being used as an attribute of a <form> tag:

<form onsubmit=”submitIt(this)” action=”submitForm.cgi”>

This example calls the submitIt function when the user clicks the form’s Submit button. You can also embed JavaScript commands in the HTML, like this:

<input type=”button” value=”Click Me!”
name=”button1” onclick=”alert(“That tickles!”);” />

A third way to use event handlers is to express them in JavaScript code, like this:

document.button1.onclick = function () { alert(“That tickles!”)}

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

HTML 4 Topics