EVENTS AND EVENT HANDLERS

An event is some action that occurs in a browser.

These actions can be generated:

  • by the user, such as moving the cursor around the page or clicking and dragging objects on the page
  • by the browser, such as loading or refreshing a page

Events can be combined with event handlers . An event handler is a block of JavaScript code that detects and responds to an event. The code is executed when the event occurs. For example, when a user moves the cursor across an image, this can trigger a dynamic change to the image source so that a new image displays.

Event handlers start with the prefix on; for example, onclick.

Not all events can be used in every tag. Many events are specific to form elements, such as the submit event that occurs when a form's Submit button is clicked.

Every new browser version includes more support for event handlers. Commonly used event handlers include:

  • onblur: user changes focus to a different element
  • onclick: element has been clicked
  • onfocus: user focuses on the element, for example, puts the cursor in a form text box
  • onload: document is loaded in a window or frame
  • onmouseout: mouse has moved away from an element
  • onmouseover: mouse has moved over an element
  • onsubmit: submit button has been clicked