Exforsys

H I D E

Home arrow Technical Training arrow JavaScript Tutorial

JavaScript Events Handling

Author : Exforsys Inc.     Published on: 20th May 2007

JavaScript Events Handling

In this JavaScript tutorial, you will learn about handling events in JavaScript, what is event handling in JavaScript? events in JavaScript, events associated with mouse - onmousemove, onclick, ondblclick, onmouseout, onmouseover, events associated with keyboard - onkeydown, onkeyup, onkeypress. onerror, onfocus, onblur, onsubmit, onload and onunload.

What is Event Handling in JavaScript?

This is a very vital concept of JavaScript because without events there would be no code. Event handling is the execution of code for the user’s reaction. In other words, when a user performs some action, the associated event fires or executes. For example, if a programmer want a piece of code to execute when a user presses a button, then the code is placed in the onclick event of the button, executing the code when the user clicks that button. In addition, there are events in JavaScript that are automatically fired without the intervention of the user. The load event fires when the page loads. Thus, there are various scenarios for firing events such as:

  • When the user performs some action based on which event fires
  • When the page load event fires
  • When some fields change, the associated events fire.

Events in JavaScript:

There are numerous events in JavaScript, some of which are listed below. A particular object has numerous events associated with it, depending on the action taken by the user.

For example, the object mouse has numerous events associated with it which depend on the user’s actions.

Events associated with Mouse:

onmousemove:

If the user moves a button, then the events associated with onmousemove fire.

onclick:

onclick events fire when the mouse button clicks.

ondblclick:

The event ondblclick fires when the mouse button is double clicked.

There are also some events associated with the mouse pointer position such as:

onmouseout:

onmouseout event fires if the mouse pointer position is out of focus from the element.

onmouseover:

onmouseover event fires if the mouse pointer position is in focus of the element position.

The above are some of the various mouse events available in JavaScript.

Events associated with Keyboard:

  • onkeydown
  • onkeyup
  • onkeypress

onkeydown:

onkeydown event fires when key is pressed.

onkeyup:

onkeyup event fires when key is released.

onkeypress:

The event onkeypress fires if the onkeydown is followed by onkeyup.

There are many additional events available in JavaScript. A few are listed below:

onerror:

onerror event fires when an error occurs.

onfocus:

When a element gains focus, onfocus event fires or executes.

onblur:

In contrast to an onfocus event, this event fires when the element loses its focus. Both onfocus and onblur may be used for handling validation of forms.

onsubmit:

The event onsubmit fires when the command form submit is given. This event is used for validating all fields in the form before submitting the form.

Ads

onload:

onload event automatically executes as soon as the document fully loads. This loads when the user enters the page. This is a commonly used event. This event is used to check compatibility with the browser version and type. Based on this compatibility, the appropriate version of a page will then load.

onunload:

In contrast to onload event, the onunload event fires when the user leaves the page.

Read Next: JavaScript Event Handler


 
This tutorial is part of a JavaScript Tutorial tutorial series. Read it from the beginning and learn yourself.

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction
 

Comments