Javascript Event
Javascript Event
Input Events
onblur - When a user leaves an input field
onchange - When a user changes the content of an input field
onchange - When a user selects a dropdown value
onfocus - When an input field gets focus
onselect - When input text is selected
onsubmit - When a user clicks the submit button
onreset - When a user clicks the reset button
onkeydown - When a user is pressing/holding down a key
onkeypress - When a user is pressing/holding down a key
onkeyup - When the user releases a key
onkeyup - When the user releases a key
onkeydown vs onkeyup - Both
Mouse Events
onmouseover/onmouseout - When the mouse passes over an
element
onmousedown/onmouseup - When pressing/releasing a mouse
button
onmousedown - When mouse is clicked: Alert which element
onmousedown - When mouse is clicked: Alert which button
onmousemove/onmouseout - When moving the mouse pointer
over/out of an image
onmouseover/onmouseout - When moving the mouse over/out
of an image
onmouseover an image map
Click Events
Acting to the onclick event
onclick - When button is clicked
ondblclick - When a text is double-clicked
1
Load Events
onload - When the page has been loaded
onload - When an image has been loaded
onerror - When an error occurs when loading an image
onunload - When the browser closes the document
onresize - When the browser window is resized
Others
What is the keycode of the key pressed?
What are the coordinates of the cursor?
What are the coordinates of the cursor, relative to the screen?
Was the shift key pressed?
Which event type occurred?
Input Events
(1) onfocus - When an input field gets focus
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction(x) {
x.style.background = "yellow";
}
</script>
</head>
2
<body>
</body>
</html>
<p id="demo"></p>
</body>
</html>
Mouse Events
(1) onmouseover/onmouseout - When the mouse passes
over an element
<!DOCTYPE html>
<html>
<body>
<h1 onmouseover="style.color='red'"
onmouseout="style.color='black'">Mouse over this text</h1>
4
</body>
</html>
(2)
Click Events
5
<p ondblclick="myFunction()">Doubleclick this paragraph to
trigger a function.</p>
<p id="demo"></p>
</body>
</html>
(2)
Load Events
(1) onload - When the page has been loaded
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
alert("Page is loaded");
}
</script>
</head>
6
<body onload="myFunction()">
<h2>Hello World!</h2>
</body>
</html>
(2)
Others events
7
<body onkeyup="whichButton(event)">
<p id="demo"></p>
</body>
</html>
Example:
<!DOCTYPE html>
<html>
<head>
<script>
function displayDate()
{
document.getElementById("demo").innerHTML = Date();
8
}
</script>
</head>
<body>
</body>
</html>
Example:
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML = "Hello World";
9
}
</script>
</head>
<body>
<p id="demo"></p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
10
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
</head>
<body>
<p id="demo"></p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert("Page is loaded");
11
}
</script>
</head>
<body onload="myFunction()">
<h2>Hello World!</h2>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<script>
function loadImage()
{
alert("Image is loaded");
}
</script>
</head>
<body>
12
<img src="w3javascript.gif" onload="loadImage()" width="100"
height="132">
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<script>
function imgError() {
alert('The image could not be loaded.');
}
</script>
</head>
<body>
13
<p>A function is triggered if an error occurs when loading the image.
The function shows an alert box with a text.
In this example we refer to an image that does not exist, therefore the
onerror event occurs.</p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
var w = window.outerWidth;
var h = window.outerHeight;
var txt = "Window size: width=" + w + ", height=" + h;
document.getElementById("demo").innerHTML = txt;
}
</script>
</head>
14
<body onresize="myFunction()">
<p>Note: this example will not work properly in IE8 and earlier. IE8
and earlier do not support the outerWidth/outerHeight propery of the
window object.</p>
</body>
</html>
15