14.JS Events
14.JS Events
By using JavaScript, we have the ability to create dynamic web pages. Events
are actions that can be detected by JavaScript.
Every element on a web page has certain events which can trigger a
JavaScript. For example, we can use the onClick event of a button element to
indicate that a function will run when a user clicks on the button. We define
the events in the HTML tags.
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 field in an HTML form
Submitting an HTML form
A keystroke
Note: Events are normally used in combination with functions, and the function
will not be executed before the event occurs!
onLoad and onUnload
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. For example,
you could have a popup asking for the user's name upon his first arrival to
your page. The name is then stored in a cookie. Next time the visitor arrives
at your page, you could have another popup saying something like: "Welcome
John Doe!".
onFocus, onBlur and onChange
The onFocus, onBlur and onChange events are often used in combination
with validation of form fields.
The checkEmail() function will be called whenever the user changes the
content of the field:
<input type="text" size="30" id="email" onchange="checkEmail()" />
<!doctype html>
<html> onclick, ondblclick,
<head> onmousemove
<script type="text/javascript"> onmouseover
function message() { onmouseout
alert("You clicked me!")
}
function mmove() {
document.getElementById("heading2").style.fontSize = "26px";
}
function mout() {
document.getElementById("heading2").style.fontSize = "18px";
}
function OnMouseIn () {
document.getElementById("one").style.border = "4px solid blue";
}
function OnMouseOut () {
document.getElementById("one").style.border = "";
}
</script>
<body>
<input type="button" onclick="message()" value="Click once" />
<p>
A function is called when the button is clicked. The function gives a message.
</p>
<br />
<input type="button" value="Double Click" ondblclick="message()" />
<p>
A function is called when the button is double clicked. The function gives a message.
</p>
<br />
<h3 onmousemove="mmove()" onmouseout="mout()" id="heading2">
When you move the mouse on this heading the size of the text will increase and when
mouse out the text size will decrease.
</h3>
<br />
<div id="one"style="background-color:pink; width:200px"
onmouseover="OnMouseIn ()" onmouseout="OnMouseOut ()">
Move your mouse pointer into and out of this element!
</div>
<br />
</body>
</html>
<!DOCTYPE html>
<html> onmousedown onmouseup
<body>
<center>
<div onmousedown="mDown(this)" onmouseup="mUp(this)"
style="background-
color:coral;width:90px;height:20px;padding:40px;">
Click Me</div>
<script>
function mDown(obj) {
obj.style.backgroundColor = "#1ec5e5";
obj.innerHTML = "HI";
}
function mUp(obj) {
obj.style.backgroundColor="pink";
obj.innerHTML="How are you!";
}
</script>
</center>
</body>
</html>
<!DOCTYPE html>
<html>
onkeyup
<head>
<script>
function myFunction() {
var x = document.getElementById("fname");
//x.value = x.value.toUpperCase();
document.getElementById("fname").value = x.value.toUpperCase();
}
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html> onkeypress
<head>
<script>
function myFunction() {
alert("You pressed a key inside the input field");
}
</script>
</head>
<body>
</body>
</html>
Form events
<!DOCTYPE html>
<html >
<head>
<title>Form Events</title>
</head>
<body>
<form action="1.html" method="post" onsubmit=" submitFunction()"
onreset="resetfun()">
function blurFunction()
{
document.getElementById("one").style.background = "";
}
/* JavaScript Input Event */
function print()
{
var x = document.getElementById("two").value;
document.getElementById("test").innerHTML = "You name is: " + x;
}
function onchangeFunction()
{
var x = document.getElementById("one").value;
document.getElementById("test").innerHTML = "You changed: " + x;
}