JavaScript Events
JavaScript Events
Event Handling is a software routine that processes actions, such as keystrokes and
mouse movements.
It is the receipt of an event at some event handler from an event producer and
subsequent processes.
1. onclick events: This is a mouse event and provokes any logic defined if the user
clicks on the element it is bound to.
Code #1:
<!doctype html>
<html>
<head>
<script>
function hiThere() {
alert('Hi there!');
}
</script>
</head>
<body>
<button type="button" onclick="hiThere()">Click me event</button>
</body>
</html>
<!doctype html>
<html>
<head>
<script>
function hov() {
var e = document.getElementById('hover');
e.style.display = 'none';
}
</script>
</head>
<body>
<div id="hover" onmouseover="hov()"
style="background-color:green;height:200px;width:200px;">
</div>
</body>
</html>
Green square gets disappear after mouse is taken over it.
4. onmouseout event: Whenever the mouse cursor leaves the element which
handles a mouseout event, a function associated with it is executed.
Code #4:
<!doctype html>
<html>
<head>
<script>
function out() {
var e = document.getElementById('hover');
e.style.display = 'none';
}
</script>
</head>
<body>
<div id="hover" onmouseout="out()"
style="background-color:green;height:200px;width:200px;">
</div>
</body>
</html>
Green square will disappear after mouse is taken over it and removed after some
time.
5. onchange event: This event detects the change in value of any element listing to
this event.
Code #5:
<!doctype html>
<html>
<head></head>
<body>
<input onchange="alert(this.value)" type="number">
</body>
</html>
Event Handlers
Event Description
Handler
onBlur It executes when the input focus leaves the field of a text, textarea or a select
option.
onChange It executes when the input focus exits the field after the user modifies its text.
onFocus It executes when input focus enters the field by tabbing in or by clicking but
not selecting input from the field.
onMouseOver The JavaScript code is called when the mouse is placed over a specific link
or an object.
onMouseOut The JavaScript code is called when the mouse leaves a specific link or an
object.
onReset It executes when the user resets a form by clicking on the reset button.
onSelect It executes when the user selects some of the text within a text or textarea
field.
// load.js
// An example to illustrate the load event
function load_greeting () {
alert("You are visiting the home page of \n" +
"Pete's Pickled Peppers \n" + "WELCOME!!!");
}