SlideShare a Scribd company logo
2
Most read
3
Most read
5
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
Javascript event handler
Jesus Obenita Jr.
 
PDF
jQuery for beginners
Arulmurugan Rajaraman
 
PDF
JavaScript - Chapter 11 - Events
WebStackAcademy
 
PPTX
Document Object Model (DOM)
GOPAL BASAK
 
PPTX
Form Validation in JavaScript
Ravi Bhadauria
 
PDF
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
PPTX
Javascript functions
Alaref Abushaala
 
Javascript event handler
Jesus Obenita Jr.
 
jQuery for beginners
Arulmurugan Rajaraman
 
JavaScript - Chapter 11 - Events
WebStackAcademy
 
Document Object Model (DOM)
GOPAL BASAK
 
Form Validation in JavaScript
Ravi Bhadauria
 
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
Javascript functions
Alaref Abushaala
 

What's hot (20)

PPTX
Lab #2: Introduction to Javascript
Walid Ashraf
 
PPT
Introduction to JavaScript
Andres Baravalle
 
PDF
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
PPT
Js ppt
Rakhi Thota
 
PPTX
Data types in java
HarshitaAshwani
 
PPTX
PHP FUNCTIONS
Zeeshan Ahmed
 
PDF
javascript objects
Vijay Kalyan
 
PPT
JavaScript Tutorial
Bui Kiet
 
PPT
Javascript arrays
Hassan Dar
 
PDF
Javascript basics
shreesenthil
 
PPT
Introduction to JavaScript (1).ppt
MuhammadRehan856177
 
PPSX
Javascript variables and datatypes
Varun C M
 
PPTX
Java script errors &amp; exceptions handling
AbhishekMondal42
 
PPTX
JSON: The Basics
Jeff Fox
 
PPTX
Dom(document object model)
Partnered Health
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PPTX
Javascript
Nagarajan
 
PPTX
Hibernate ppt
Aneega
 
Lab #2: Introduction to Javascript
Walid Ashraf
 
Introduction to JavaScript
Andres Baravalle
 
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Js ppt
Rakhi Thota
 
Data types in java
HarshitaAshwani
 
PHP FUNCTIONS
Zeeshan Ahmed
 
javascript objects
Vijay Kalyan
 
JavaScript Tutorial
Bui Kiet
 
Javascript arrays
Hassan Dar
 
Javascript basics
shreesenthil
 
Introduction to JavaScript (1).ppt
MuhammadRehan856177
 
Javascript variables and datatypes
Varun C M
 
Java script errors &amp; exceptions handling
AbhishekMondal42
 
JSON: The Basics
Jeff Fox
 
Dom(document object model)
Partnered Health
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Javascript
Nagarajan
 
Hibernate ppt
Aneega
 
Ad

Similar to Event In JavaScript (20)

PPTX
Javascript 2
pavishkumarsingh
 
PDF
Web 5 | JavaScript Events
Mohammad Imam Hossain
 
PPTX
Learn Javascript Basics
Khushiar
 
PPTX
DHTML - Events & Buttons
Deep Patel
 
PPT
Document_Object_Model_in_javaScript..................................ppt
rahamatmandal2005
 
PPT
Event Programming JavaScript
MuhammadRehan856177
 
PPT
javascript Event Handling and introduction to event.ppt
Lalith86
 
PPTX
Upstate CSCI 450 WebDev Chapter 4
DanWooster1
 
PPS
CS101- Introduction to Computing- Lecture 32
Bilal Ahmed
 
PPTX
types of events in JS
chauhankapil
 
PPTX
JavaScript_Event_Handling_Updated_______
Captain81145
 
PPTX
JavaScript_Event_Handling_Presentation.pptx
Captain81145
 
PPTX
5 .java script events
chauhankapil
 
PPTX
javascript-events_zdgdsggdgdgdsggdgdgd.pptx
NetajiGandi1
 
PPTX
FYBSC IT Web Programming Unit III Events and Event Handlers
Arti Parab Academics
 
PDF
Events.pdf
stephanedjeukam1
 
PPTX
Upstate CSCI 450 WebDev Chapter 9
DanWooster1
 
PPTX
JavaScript_Events.pptx
Yagna15
 
PPTX
Web programming
Subha Selvam
 
PPT
Learn javascript easy steps
prince Loffar
 
Javascript 2
pavishkumarsingh
 
Web 5 | JavaScript Events
Mohammad Imam Hossain
 
Learn Javascript Basics
Khushiar
 
DHTML - Events & Buttons
Deep Patel
 
Document_Object_Model_in_javaScript..................................ppt
rahamatmandal2005
 
Event Programming JavaScript
MuhammadRehan856177
 
javascript Event Handling and introduction to event.ppt
Lalith86
 
Upstate CSCI 450 WebDev Chapter 4
DanWooster1
 
CS101- Introduction to Computing- Lecture 32
Bilal Ahmed
 
types of events in JS
chauhankapil
 
JavaScript_Event_Handling_Updated_______
Captain81145
 
JavaScript_Event_Handling_Presentation.pptx
Captain81145
 
5 .java script events
chauhankapil
 
javascript-events_zdgdsggdgdgdsggdgdgd.pptx
NetajiGandi1
 
FYBSC IT Web Programming Unit III Events and Event Handlers
Arti Parab Academics
 
Events.pdf
stephanedjeukam1
 
Upstate CSCI 450 WebDev Chapter 9
DanWooster1
 
JavaScript_Events.pptx
Yagna15
 
Web programming
Subha Selvam
 
Learn javascript easy steps
prince Loffar
 
Ad

More from ShahDhruv21 (12)

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

Recently uploaded (20)

PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
Production of bioplastic from fruit peels.pptx
alwingeorgealwingeor
 
PPTX
EE3303-EM-I 25.7.25 electrical machines.pptx
Nagen87
 
PPTX
TE-AI-Unit VI notes using planning model
swatigaikwad6389
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PPTX
Module_II_Data_Science_Project_Management.pptx
anshitanarain
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
demidovs1
 
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
VinayB68
 
PDF
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
dodultrongaming
 
PDF
Introduction to Data Science: data science process
ShivarkarSandip
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
easa module 3 funtamental electronics.pptx
tryanothert7
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Production of bioplastic from fruit peels.pptx
alwingeorgealwingeor
 
EE3303-EM-I 25.7.25 electrical machines.pptx
Nagen87
 
TE-AI-Unit VI notes using planning model
swatigaikwad6389
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
Module_II_Data_Science_Project_Management.pptx
anshitanarain
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
demidovs1
 
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
VinayB68
 
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
dodultrongaming
 
Introduction to Data Science: data science process
ShivarkarSandip
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
easa module 3 funtamental electronics.pptx
tryanothert7
 

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