A JavaScript event is something that triggers a specific action in the browser. Normally an event occurs when a user loads a page.
Following are some examples when an JavaScript event occurs:
When the page loads
When a user moves her mouse
When the user clicks a button
When the user scrolls the page
When the user clicks a form field
How events work?
When a JavaScript event occurs to an HTML element in a web page, it checks to see if any event handlers are attached to it. If the answer is yes then it calls them in respective order, while sending along with references and further information for each event that occurred. The event handlers then act on the event.
The JavaScript event is categorized into four main groups:
Mouse events
Keyboard events
Form events
Document/Windows events
Mouse Events:
A mouse event occurs when the user clicks some element, moves the mouse pointer over an element, etc.
Following are some important mouse events:
The onclick Event:
When a user clicks on an element on a webpage the click event occurs and you can handle a click event with an onclick event handler.
The following example shows an alert message when you click on the elements.
Example:
<button type="button" onclick="alert('Welcome! Learn JavaScript events');"> Click Me </button>
The contextmenu event triggers when a user clicks the right button of the mouse on an element to open the context menu and you can handle a contextmenu event with an oncontextmenu event handler.
The following example shows an alert message when you right-click on the event.
Example:
<button type="button" oncontextmenu="alert('You have clicked right button of mouse');"> Right Click on Me </button>
The mouseover event triggers when a user moves the mouse pointer over an element and
you can handle the mouseover event with the onmouseover event handler.
The following example shows an alert message when you mouse over the elements.
Example:
<button type="button" onmouseover="alert('You have placed mouse pointer over a button!');"> Mouse Over Me </button>
The mouseout event occurs when a user moves the mouse pointer outside of an element.
The mouseout event can be handled with the onmouseout event handler.
The following example shows an alert message when the mouseout event occurs.
Example:
<button type="button" onmouseout="alert('You have moved out of the button!');"> Put Mouse Over Me and Move Out </button>
The keypress event occurs when a user presses down a key on the keyboard that has a character value associated with it. For example, keys like ctrl, shift, alt, esc, and arrow keys, etc. will not generate a keypress event, but generate a keydown and keyup event.
The keypress event can be handled with the onkeypress event handler.
The following example shows an alert message when the keypress event occurs.
Example:
<input type="text" onkeypress="alert('You have pressed a key inside text input!')">
A form event occurs when a form control receives or loses focus or when the user modifies a form control value such as by typing text in a text input, select an option in a select box, etc.
The following are some important form events and their event handlers.
The onfocus Event:
The focus event triggers when the user gives focus to an element on a web page.
The focus event can be handled with the onfocus event handler.
The following example highlights the background of text input in yellow color when it receives the focus.
The blur event triggers when the user takes the focus away from a form element or a window.
The blur event can be handled with the onblur event handler.
The following example shows an alert message when the text input element loses focus.
The resize event triggers when a user resizes the browser window and it also occurs in situations when the browser window is minimized or maximized.
The resize event can be handled with the onresize event handler.
The following example shows an alert message when you resize the browser window to a new width and height.
Example:
<script>
function displayWindowSize(){
var w = window.outerWidth;
var h = window.outerHeight;
var txt = "Window size: width=" + w + ", height=" + h;
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok