Events & Event Handlers
• Every element on a web page has certain events which can trigger invocation of event handlers
• Attributes are inserted into HTML tags to define events and event handlers
• Examples of events
> A mouse click
> A web page or an image loading
> Mousing over a hot spot on the web page
> Selecting an input box in an HTML form
> Submitting an HTML form
> A keystroke
Events
• onabort - Loading of an image is interrupted
• onblur - An element loses focus
• onchange - The content of a field changes
• onclick - Mouse clicks an object
• ondblclick - Mouse double-clicks an object
• onerror - An error occurs when loading a document or an image
• onfocus - An element gets focus
• onkeydown - A keyboard key is pressed
Events
• onkeypress - A keyboard key is pressed or held down
• onkeyup - A keyboard key is released
• onload - A page or an image is finished loading
• onmousedown - A mouse button is pressed
• onmousemove - The mouse is moved
• onmouseout - The mouse is moved off an element
• onmouseover - The mouse is moved over an element
• onmouseup - A mouse button is released
Events
• onreset - The reset button is clicked
• onresize - A window or frame is resized
• onselect - Text is selected
• onsubmit - The submit button is clicked
• onunload - The user exits the page
onLoad & onUnload Events
• The onload and onUnload events are triggered when the user enters or leaves the page
• The onload event is often used to check the visitor's browser type and browser version, and load the proper version of the web page based on the information
• Both the onload and onUnload events are also often used to deal with cookies that should be set when a user enters or leaves a page.
onFocus, onBlur and onChange
• The onFocus, onBlur and onChange events are often used in combination with validation of form fields.
• Example: The checkEmail() function will be called whenever the user changes the content of the field:
<input type="text" size="30"
id="email" onchange="checkEmail()">;
Example & Demo: onblur
<html>
<head>
<script type="text/javascript">
function upperCase() {
var x=document.getElementById("fname").value document.getElementById("fname").value=x.toUpperCase()
}
</script>
</head>
<body>
Enter your name:
<input type="text" id="fname" onblur="upperCase()">
</body>
</html>
onSubmit
• The onSubmit event is used to validate all form fields before submitting it.
• Example: The checkForm() function will be called when the user clicks the submit button in the form. If the field values are not accepted, the submit should be canceled. The function checkForm() returns
either true or false. If it returns true the form will be
submitted, otherwise the submit will be cancelled:
<form method="post" action="xxx.html" onsubmit="return checkForm()">
Example & Demo: onSubmit
<html>
<head>
<script type="text/javascript">
function validate() {
// return true or false based on validation logic
}
</script>
</head>
<body>
<form action="tryjs_submitpage.htm" onsubmit="return validate()">
Name (max 10 chararcters): <input type="text" id="fname" size="20"><br /> Age (from 1 to 100): <input type="text" id="age" size="20"><br />
E-mail: <input type="text" id="email" size="20"><br />
<br />
<input type="submit" value="Submit">
</form>
</body>
</html>
onMouseOver and onMouseOut
• onMouseOver and onMouseOut are often used to create "animated" buttons.
• Below is an example of an onMouseOver event. An alert box appears when an onMouseOver event is detected:
<a href="http://www.w3schools.com"
onmouseover="alert('An onMouseOver event');return false">
<img src="w3schools.gif" width="100" height="30">
</a>
SEE VIDEO
0 Responses So Far: