Java Script L20
Java Script L20
Events
• HTML events are "things" that happen to HTML elements.
• When JavaScript is used in HTML pages, JavaScript
can "react" on these events.
• An HTML event can be something the browser does, or
something a user does.
• JavaScript lets you execute code when events are detected.
• Here are some examples of HTML events:
An HTML web page has finished loading
An HTML input field was changed
An HTML button was clicked
Types of Events
• Form Events
• Keyboard Events
• Mouse Events
Form Events
• onblur Event attribute
• onchange Event Attribute
• onfocus Event Attribute
• oninput Event Attribute
• oninvalid Event Attribute
• onreset Event Attribute
• onselect Event Attribute
• onsubmit Event Attribute
onblur Event attribute
<!DOCTYPE html>
<html>
<body>
<p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>
<script>
function myFunction() {
var x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
onchange Event Attribute
<!DOCTYPE html>
<html>
<body>
<p>When you select a new car, a function is triggered which outputs the value of the selected car.</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "You selected: " + x;
}
</script>
</body>
</html>
onfocus Event Attribute
<!DOCTYPE html>
<html>
<body>
<p>A function is triggered when one of the input fields get focus. The function changes the background-
color of the input field.</p>
First name: <input type="text" id="fname" onfocus="myFunction(this.id)"><br>
Last name: <input type="text" id="lname" onfocus="myFunction(this.id)">
<script>
function myFunction(x) {
document.getElementById(x).style.background = "yellow";
}
</script>
</body>
</html>
oninput Event Attribute
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myInput").value;
document.getElementById("demo").innerHTML = "You wrote: " + x;
}
</script>
</body>
</html>
oninvalid Event Attribute
<!DOCTYPE html>
<html>
<body>
<p>If you click submit, without filling out the text field, an alert message will occur.</p>
</body>
</html>
onreset Event Attribute
<!DOCTYPE html>
<html>
<body>
<p>When you reset the form, a function is triggered which alerts some text.</p>
<form onreset="myFunction()">
Enter name: <input type="text">
<input type="reset">
</form>
<script>
function myFunction() {
alert("The form was reset");
}
</script>
</body>
</html>
onsearch Event Attribute
<!DOCTYPE html>
<html>
<body>
<p><strong>Note:</strong> The onsearch event is not supported in Internet Explorer, Firefox or Opera 12 and earlier versions.</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myInput");
document.getElementById("demo").innerHTML = "You are searching for: " + x.value;
}
</script>
</body>
</html>
onselect Event Attribute
<!DOCTYPE html>
<html>
<body>
<p>The function myFunction() is triggered when some text is selected in the input field. The function
shows a message.</p>
<script>
function myFunction() {
alert("You have selected some text!");
}
</script>
</body>
</html>
onsubmit Event Attribute
<!DOCTYPE html>
<html>
<body>
<p>When you submit the form, a function is triggered which alerts some text.</p>
<script>
function myFunction() {
alert("The form was submitted");
}
</script>
</body>
</html>
Keyboard Events
• onkeypress Event Attribute
• onkeyup Event Attribute
onkeypress Event Attribute
<!DOCTYPE html>
<html>
<body>
<p>A function is triggered when the user is pressing a key in the input field.</p>
<script>
function myFunction() {
alert("You pressed a key inside the input field");
}
</script>
</body>
</html>
onkeyup Event Attribute
<!DOCTYPE html>
<html>
<body>
<p>A function is triggered when the user releases a key in the input field. The function transforms the
character to upper case.</p>
Enter your name: <input type="text" id="fname" onkeyup="myFunction()">
<script>
function myFunction() {
var x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
Mouse Events
• onclick Event Attribute
• ondblclick Event Attribute
• onmousedown Event Attribute
• onmousemove Event Attribute
• onwheel Event Attribute
onclick Event Attribute
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<p>A function is triggered when the button is clicked. The function outputs some text in a p element with
id="demo".</p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
</body>
</html>
ondblclick Event Attribute
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<p>A function is triggered when the button is double-clicked. The function outputs some text in a p element with
id="demo".</p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
</body>
</html>
onmousedown Event Attribute
<!DOCTYPE html>
<html>
<body>
<script>
function mouseDown() {
document.getElementById("p1").style.color = "red";
}
function mouseUp() {
document.getElementById("p1").style.color = "green";
}
</script>
</body>
</html>
onmousemove Event Attribute
<!DOCTYPE html>
<html>
<body>
<p>The function bigImg() is triggered when the user mouse pointer is moved over the image. This function enlarges the image.</p>
<p>The function normalImg() is triggered when the mouse pointer is moved out of the image. That function sets the height and width of the image back
to normal.</p>
<script>
function bigImg(x) {
x.style.height = "64px";
x.style.width = "64px";
}
function normalImg(x) {
x.style.height = "32px";
x.style.width = "32px";
}
</script>
</body>
</html>
onwheel Event Attribute
<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {
border: 1px solid red;
}
</style>
</head>
<body>
<div id="myDIV" onwheel="myFunction()">This example demonstrates how to assign an "onwheel" event event to a DIV element. Roll the mouse wheel over me -
either up or down!</div>
<p>A function is triggered when you roll the mouse wheel over div. The function sets the font-size of div to 35 pixels.</p>
<script>
function myFunction() {
document.getElementById("myDIV").style.fontSize = "35px";
}
</script>
</body>
</html>