JavaScript Events
What is an Event
-
JavaScript interaction with HTML handled through events.
-
When the page loads, it is called an event. When the user clicks too is an event. Other examples include events like pressing any key, closing a Window, resizing a Window, etc.
-
HTML events are “things” that happen to html elements.
-
When JavaScript is used in HTML pages, Java can “react” to these events.
Event-driven Programs
-
Programs that can capture and respond to events are called ‘event-driven programs’
-
JavaScript was specifically designed for writing such programs
-
Almost all programs written in JavaScript are driven.
JavaScript lets you execute code when events are detected.
Html allows event handler attributes, with JavaScript code, to be added to html elements.
Syntax
< some-HTML-element some-event=’Some JavaScript’>
Common HTML Events
There are some common HTML Events are as below table:
Event
|
Description
|
onchange
|
An HTML element has been changed
|
onclick
|
The user clicks an HTML element
|
onmouseover
|
The user moves the mouse over an HTML element
|
onmouseout
|
The user moves the mouse away from an HTML
element
|
onkeydown
|
The user pushes a keyboard key
|
onload
|
The browser has finished loading the page
|
Summit
|
Fires When a form is submitted
|
Select
|
Fires when a text selection begins
|
resize
|
Fires When the size of an object changes
|
reset
|
Fire when form resets
|
Event Handler
The function that’s called when an event occurs is known as the event handler.
When a GUI (Graphical User Interface) event occurs in a form , the browser calls the specified event handling function.
Before any event can be processed, each element must know which event –handling function will be called when a particular event occurs.