100% found this document useful (1 vote)
47 views15 pages

14.JS Events

JavaScript can be used to create dynamic web pages by detecting events like mouse clicks, page loads, and form submissions. Common events include onclick, onload, onsubmit, onfocus, onblur, and onkeypress. Events are often used with functions to trigger code when the event occurs, like displaying an alert popup on a button click. Form field validation is also commonly done using onfocus, onblur, and onchange events.

Uploaded by

Raj Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
47 views15 pages

14.JS Events

JavaScript can be used to create dynamic web pages by detecting events like mouse clicks, page loads, and form submissions. Common events include onclick, onload, onsubmit, onfocus, onblur, and onkeypress. Events are often used with functions to trigger code when the event occurs, like displaying an alert popup on a button click. Form field validation is also commonly done using onfocus, onblur, and onchange events.

Uploaded by

Raj Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

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>

Enter your name: <input type="text" id="fname" onkeyup="myFunction()">

</body>
</html>
<!DOCTYPE html>
<html> onkeypress
<head>
<script>
function myFunction() {
alert("You pressed a key inside the input field");
}
</script>
</head>
<body>

<input type="text" onkeypress="myFunction()">

</body>
</html>
Form events
<!DOCTYPE html>
<html >
<head>
<title>Form Events</title>
</head>
<body>
<form action="1.html" method="post" onsubmit=" submitFunction()"
onreset="resetfun()">

<label for="">Name</label><input type="text" id="one" onfocus="focusFunction()"


onblur="blurFunction()" onchange="onchangeFunction()"
onselect="selectFunction()" oninvalid="alert('Please fill the First Name.')"
required><br><br>

<label for="">Class</label><input type="text" id="" ><br><br>


<p><strong>Note:</strong> Select any option in select box to see how it
works.</p>
<select onchange="alert('You have changed the selection!')">
<option >India</option>
<option>Pakistan</option>
<option >Bangladesh</option>
<option >Sri Lanka</option>
<option >Nepal</option>
</select>
<input type="submit" >
<input type="reset">
</form>

<div id="test" style="border:1px solid red;margin-top:20px;"></div>


<script>
function focusFunction()
{
document.getElementById("one").style.background = "coral";
}

/* JavaScript Blur Event */

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;
}

/* JavaScript Select Event */


function selectFunction()
{
alert("You selected some text.");
}
function submitFunction()
{
var x = document.getElementById("one").value;
alert("Hello " + x);
alert('Form data will be submitted to the server!');
}
function resetfun() {
alert("The form was reset");
}
</script>
</body>
</html>

You might also like