SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
JAVA SCRIPT EVENTS AND EVENT LISTENERS
A. D. PATEL INSTITUTE OF TECHNOLOGY
WEB TECHNOLOGY(2160708) : A.Y. 2018-19
GUIDED BY:
PROF. HEMANSHU A. PATEL
(DEPT OF IT, ADIT)
PREPARED BY:
KUNAL KATHE
E.R.NO.:160010116021
SHAH DHRUV
E.R NO.:160010116053
CHINTAN SUDANI
E.R.NO.:160010116056
PATEL RUSHIL
E.R.NO.:170014116001
B.E. (IT) SEM - VI
DEPARTMENT OF INFORMATION TECHNOLOGY
A D PATEL INSTITUTE OF TECHNOLOGY (ADIT)
NEW VALLABH VIDYANAGAR, ANAND, GUJARAT
2
Introduction
• Event-driven: code executed resulting to user or browser action.
• Event: a notification that something specific occurred -- by browser or user.
• Event handler: a script implicitly executed in response to event occurrence.
• Registration: the process of connecting event handler to event.
• Events are JavaScript objects --> names are case sensitive, all use lowercase only.
(Method write should never be used in event handler. May cause document to be written
over.)
• JavaScript events associated with HTML tag attributes which can be used to connect to
event-handlers
3
• JavaScript's interaction with HTML is handled through events that occur when the user or
the browser manipulates a page.
• When the page loads, it is called an event. When the user clicks a button, that click too is an
event,Other examples include events like pressing any key, closing a window, resizing a
window, etc.
• Developers can use these events to execute JavaScript coded responses, which cause buttons
to close windows, messages to be displayed to users, data to be validated, and virtually any
other type of response imaginable.
• Events are a part of the Document Object Model DOM Level 3 and every HTML element
contains a set of events which can trigger JavaScript Code.
4
onclick Event Type
• This is the most frequently used event type which occurs when a user clicks the left button of
his mouse. You can put your validation, warning etc., against this event type.
• One attribute can appear in several different tags:
e.g. onClick can be in <a> and <input>
• HTML element get focus:
1. When user puts mouse cursor over it and presses the left button
2. When user tabs to the element
3. By executing the focus method
4. Element get blurred when another element gets focus
5
• Event handlers can be specified two ways
1. Assigning the event handler script to an event tag attribute
onClick = "alert('Mouse click!');"
onClick = "myHandler();
2. Assigning them to properties of JavaScript object associated with HTML elements.
• The load event: the completion of loading of a document by browser
• The onload attribute of <body> used to specify event handler:
• The unload event: used to clean up things before a document is unloaded.
6
Example:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
</script>
</head>
<body>
<p>Click the following button and see result</p>
<form>
<input type="button" onclick="sayHello()" value="Say Hello" />
</form>
</body>
</html>
7
onSubmit Event Type
• onSubmit is an event that occurs when you try to submit a form. You can put your form
validation against this event type.
• The following Next slide shows how to use onsubmit. Here we are calling a validate
function before submitting a form data to the webserver. If validate function returns true, the
form will be submitted, otherwise it will not submit the data.
8
Example:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
</script>
</head>
<body>
<form method="POST" action="target.html" onsubmit="return validate()">
.......
<input type="submit" value="Submit" />
</form>
</body>
</html>
9
onmouseover and onmouseout
• These two event types will help you create nice effects with images or even with text as
well.
• The onmouseover event triggers when you bring your mouse over any element and the
onmouseout triggers when you move your mouse out from that element.
• Try the following example.
10
Example:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
</script>
</head>
<body>
<p>Bring your mouse inside the division to see the result:</p>
<div onmouseover="over()" onmouseout="out()">
<h2> This is inside the division </h2>
</div>
</body>
</html>
11
Event List
12
Focus & Blur Event Example:
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
</head>
<body>
<h1>Hello How Are You...?</h1>
<form>
Click This Button<br/>
<input type="button" value="Click Me!" onclick="myFun()"/><br/>
<input type="text" id="username" onfocus="this.blur()"/><br/>
</form>
<script type="text/javascript">
function myFun()
{
document.getElementById("username").value="Dhruv";
}
</script>
</body>
</html>
[fig.1 Before Click On That Button]
[fig.2 After Click On That Button]
13
addEventListener
• The Event Target method addEventListener() sets up a function that will be called
whenever the specified event is delivered to the target.
• Common targets are Element, Document, and Window, but the target may be any object that
supports events (such as XML Http Request).
• addEventListener() works by adding a function or an object that implements Event Listener
to the list of event listeners for the specified event type on the Event Target on which it's
called.
14
Syntax
target.addEventListener(type, listener[, options]);
target.addEventListener(type, listener[, useCapture]);
target.addEventListener(type, listener[, useCapture, wantsUntrusted ]);
document.getElementById("myBtn").addEventListener("click", displayDate);
15
removeEventListener
• The EventTarget.removeEventListener() method removes from the EventTarget an event
listener previously registered with EventTarget.addEventListener().
• The event listener to be removed is identified using a combination of the event type, the
event listener function itself, and various optional options that may affect the matching
process; see Matching event listeners for removal.
16
Syntax
target.removeEventListener(type, listener[, options]);
target.removeEventListener(type, listener[, useCapture]);
17
Other Example Of Events
<!DOCTYPE html>
<html>
<head><title>Display Page</title></head>
<body>
<hr color="orange" />
<center><h1 id="htag">Welcome To ADIT</h1></center>
<hr color="blue" />
<center><button type="button" onclick="Change()">Change</button>
<button type="button" onclick="Hide()">Hide</button>
<button type="button" onclick="Display()">Display</button>
<button type="button" onclick="ChangeColor()">Color Change</button></center>
<hr color="green" />
<script type="text/javascript">
function Change()
{ document.getElementById("htag").innerHTML="Welcome ABC"; }
function Display()
{ document.getElementById("htag").style.display="block"; }
function Hide()
{ document.getElementById("htag").style.display="none"; }
function ChangeColor()
{ document.getElementById("htag").style.color="blue"; }
</script>
</body>
</html>
18
Output
[fig.3 Initial Page] [fig.4 When Click On Change Or Display]
[fig.5 When Click On Hide] [fig.6 When Click On Color Change]
19

More Related Content

PPTX
Document Object Model (DOM)
PPTX
Javascript event handler
PDF
jQuery for beginners
PDF
JavaScript - Chapter 11 - Events
PPTX
Java script
PPTX
Java script
PDF
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
Document Object Model (DOM)
Javascript event handler
jQuery for beginners
JavaScript - Chapter 11 - Events
Java script
Java script
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...

What's hot (20)

PPT
Introduction to JavaScript (1).ppt
PPSX
Javascript variables and datatypes
PDF
JavaScript - Chapter 12 - Document Object Model
PPT
C# Exceptions Handling
PPTX
Event Handling in java
PPTX
Lab #2: Introduction to Javascript
PPTX
Interface in java
PPTX
Introduction to php
PDF
Basics of JavaScript
PPTX
Ajax
PPTX
MULTI THREADING IN JAVA
PPT
Javascript
PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
PPTX
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
PPT
Introduction to java beans
PPT
Introduction to Javascript
PPT
JavaScript: Events Handling
PDF
Javascript basics
PPTX
Asp.NET Validation controls
Introduction to JavaScript (1).ppt
Javascript variables and datatypes
JavaScript - Chapter 12 - Document Object Model
C# Exceptions Handling
Event Handling in java
Lab #2: Introduction to Javascript
Interface in java
Introduction to php
Basics of JavaScript
Ajax
MULTI THREADING IN JAVA
Javascript
JavaScript - Chapter 13 - Browser Object Model(BOM)
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
Introduction to java beans
Introduction to Javascript
JavaScript: Events Handling
Javascript basics
Asp.NET Validation controls
Ad

Similar to Event In JavaScript (20)

PPTX
Javascript 2
PDF
Web 5 | JavaScript Events
PPTX
Learn Javascript Basics
PPTX
DHTML - Events & Buttons
PPT
Document_Object_Model_in_javaScript..................................ppt
PPT
Event Programming JavaScript
PPT
javascript Event Handling and introduction to event.ppt
PPTX
Upstate CSCI 450 WebDev Chapter 4
PPS
CS101- Introduction to Computing- Lecture 32
PPTX
types of events in JS
PPTX
JavaScript_Event_Handling_Updated_______
PPTX
JavaScript_Event_Handling_Presentation.pptx
PPTX
5 .java script events
PPTX
javascript-events_zdgdsggdgdgdsggdgdgd.pptx
PPTX
FYBSC IT Web Programming Unit III Events and Event Handlers
PDF
Events.pdf
PPTX
Upstate CSCI 450 WebDev Chapter 9
PPTX
JavaScript_Events.pptx
PPTX
Web programming
PPT
Learn javascript easy steps
Javascript 2
Web 5 | JavaScript Events
Learn Javascript Basics
DHTML - Events & Buttons
Document_Object_Model_in_javaScript..................................ppt
Event Programming JavaScript
javascript Event Handling and introduction to event.ppt
Upstate CSCI 450 WebDev Chapter 4
CS101- Introduction to Computing- Lecture 32
types of events in JS
JavaScript_Event_Handling_Updated_______
JavaScript_Event_Handling_Presentation.pptx
5 .java script events
javascript-events_zdgdsggdgdgdsggdgdgd.pptx
FYBSC IT Web Programming Unit III Events and Event Handlers
Events.pdf
Upstate CSCI 450 WebDev Chapter 9
JavaScript_Events.pptx
Web programming
Learn javascript easy steps
Ad

More from ShahDhruv21 (12)

PPTX
Semantic net in AI
PPTX
Error Detection & Error Correction Codes
PPTX
Secure Hash Algorithm (SHA)
PPTX
Data Mining in Health Care
PPTX
Data Compression in Data mining and Business Intelligencs
PPTX
MongoDB installation,CRUD operation & JavaScript shell
PPTX
2D Transformation
PPTX
Interpreter
PPTX
Topological Sorting
PPTX
Pyramid Vector Quantization
PPTX
JSP Directives
PPTX
WaterFall Model & Spiral Mode
Semantic net in AI
Error Detection & Error Correction Codes
Secure Hash Algorithm (SHA)
Data Mining in Health Care
Data Compression in Data mining and Business Intelligencs
MongoDB installation,CRUD operation & JavaScript shell
2D Transformation
Interpreter
Topological Sorting
Pyramid Vector Quantization
JSP Directives
WaterFall Model & Spiral Mode

Recently uploaded (20)

PPTX
Welding lecture in detail for understanding
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Well-logging-methods_new................
PPTX
Internet of Things (IOT) - A guide to understanding
PPT
Project quality management in manufacturing
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Construction Project Organization Group 2.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Geodesy 1.pptx...............................................
PPTX
UNIT 4 Total Quality Management .pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPT
Mechanical Engineering MATERIALS Selection
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Welding lecture in detail for understanding
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
bas. eng. economics group 4 presentation 1.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Well-logging-methods_new................
Internet of Things (IOT) - A guide to understanding
Project quality management in manufacturing
R24 SURVEYING LAB MANUAL for civil enggi
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Construction Project Organization Group 2.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Geodesy 1.pptx...............................................
UNIT 4 Total Quality Management .pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Mechanical Engineering MATERIALS Selection
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
573137875-Attendance-Management-System-original
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx

Event In JavaScript

  • 1. JAVA SCRIPT EVENTS AND EVENT LISTENERS A. D. PATEL INSTITUTE OF TECHNOLOGY WEB TECHNOLOGY(2160708) : A.Y. 2018-19 GUIDED BY: PROF. HEMANSHU A. PATEL (DEPT OF IT, ADIT) PREPARED BY: KUNAL KATHE E.R.NO.:160010116021 SHAH DHRUV E.R NO.:160010116053 CHINTAN SUDANI E.R.NO.:160010116056 PATEL RUSHIL E.R.NO.:170014116001 B.E. (IT) SEM - VI DEPARTMENT OF INFORMATION TECHNOLOGY A D PATEL INSTITUTE OF TECHNOLOGY (ADIT) NEW VALLABH VIDYANAGAR, ANAND, GUJARAT
  • 2. 2 Introduction • Event-driven: code executed resulting to user or browser action. • Event: a notification that something specific occurred -- by browser or user. • Event handler: a script implicitly executed in response to event occurrence. • Registration: the process of connecting event handler to event. • Events are JavaScript objects --> names are case sensitive, all use lowercase only. (Method write should never be used in event handler. May cause document to be written over.) • JavaScript events associated with HTML tag attributes which can be used to connect to event-handlers
  • 3. 3 • JavaScript's interaction with HTML is handled through events that occur when the user or the browser manipulates a page. • When the page loads, it is called an event. When the user clicks a button, that click too is an event,Other examples include events like pressing any key, closing a window, resizing a window, etc. • Developers can use these events to execute JavaScript coded responses, which cause buttons to close windows, messages to be displayed to users, data to be validated, and virtually any other type of response imaginable. • Events are a part of the Document Object Model DOM Level 3 and every HTML element contains a set of events which can trigger JavaScript Code.
  • 4. 4 onclick Event Type • This is the most frequently used event type which occurs when a user clicks the left button of his mouse. You can put your validation, warning etc., against this event type. • One attribute can appear in several different tags: e.g. onClick can be in <a> and <input> • HTML element get focus: 1. When user puts mouse cursor over it and presses the left button 2. When user tabs to the element 3. By executing the focus method 4. Element get blurred when another element gets focus
  • 5. 5 • Event handlers can be specified two ways 1. Assigning the event handler script to an event tag attribute onClick = "alert('Mouse click!');" onClick = "myHandler(); 2. Assigning them to properties of JavaScript object associated with HTML elements. • The load event: the completion of loading of a document by browser • The onload attribute of <body> used to specify event handler: • The unload event: used to clean up things before a document is unloaded.
  • 6. 6 Example: <!DOCTYPE html> <html> <head> <script type="text/javascript"> </script> </head> <body> <p>Click the following button and see result</p> <form> <input type="button" onclick="sayHello()" value="Say Hello" /> </form> </body> </html>
  • 7. 7 onSubmit Event Type • onSubmit is an event that occurs when you try to submit a form. You can put your form validation against this event type. • The following Next slide shows how to use onsubmit. Here we are calling a validate function before submitting a form data to the webserver. If validate function returns true, the form will be submitted, otherwise it will not submit the data.
  • 8. 8 Example: <!DOCTYPE html> <html> <head> <script type="text/javascript"> </script> </head> <body> <form method="POST" action="target.html" onsubmit="return validate()"> ....... <input type="submit" value="Submit" /> </form> </body> </html>
  • 9. 9 onmouseover and onmouseout • These two event types will help you create nice effects with images or even with text as well. • The onmouseover event triggers when you bring your mouse over any element and the onmouseout triggers when you move your mouse out from that element. • Try the following example.
  • 10. 10 Example: <!DOCTYPE html> <html> <head> <script type="text/javascript"> </script> </head> <body> <p>Bring your mouse inside the division to see the result:</p> <div onmouseover="over()" onmouseout="out()"> <h2> This is inside the division </h2> </div> </body> </html>
  • 12. 12 Focus & Blur Event Example: <!DOCTYPE html> <html> <head> <title>Demo</title> </head> <body> <h1>Hello How Are You...?</h1> <form> Click This Button<br/> <input type="button" value="Click Me!" onclick="myFun()"/><br/> <input type="text" id="username" onfocus="this.blur()"/><br/> </form> <script type="text/javascript"> function myFun() { document.getElementById("username").value="Dhruv"; } </script> </body> </html> [fig.1 Before Click On That Button] [fig.2 After Click On That Button]
  • 13. 13 addEventListener • The Event Target method addEventListener() sets up a function that will be called whenever the specified event is delivered to the target. • Common targets are Element, Document, and Window, but the target may be any object that supports events (such as XML Http Request). • addEventListener() works by adding a function or an object that implements Event Listener to the list of event listeners for the specified event type on the Event Target on which it's called.
  • 14. 14 Syntax target.addEventListener(type, listener[, options]); target.addEventListener(type, listener[, useCapture]); target.addEventListener(type, listener[, useCapture, wantsUntrusted ]); document.getElementById("myBtn").addEventListener("click", displayDate);
  • 15. 15 removeEventListener • The EventTarget.removeEventListener() method removes from the EventTarget an event listener previously registered with EventTarget.addEventListener(). • The event listener to be removed is identified using a combination of the event type, the event listener function itself, and various optional options that may affect the matching process; see Matching event listeners for removal.
  • 17. 17 Other Example Of Events <!DOCTYPE html> <html> <head><title>Display Page</title></head> <body> <hr color="orange" /> <center><h1 id="htag">Welcome To ADIT</h1></center> <hr color="blue" /> <center><button type="button" onclick="Change()">Change</button> <button type="button" onclick="Hide()">Hide</button> <button type="button" onclick="Display()">Display</button> <button type="button" onclick="ChangeColor()">Color Change</button></center> <hr color="green" /> <script type="text/javascript"> function Change() { document.getElementById("htag").innerHTML="Welcome ABC"; } function Display() { document.getElementById("htag").style.display="block"; } function Hide() { document.getElementById("htag").style.display="none"; } function ChangeColor() { document.getElementById("htag").style.color="blue"; } </script> </body> </html>
  • 18. 18 Output [fig.3 Initial Page] [fig.4 When Click On Change Or Display] [fig.5 When Click On Hide] [fig.6 When Click On Color Change]
  • 19. 19