Ensure that event handlers are input device independent for applets and scripts and that programming elements such as scripts and applets are directly accessible or compatible with assistive technologies.
(see guidelines for overcoming access barriers)
An event handler is a script that is invoked when a certain event occurs (e.g, the mouse moves, a key is pressed, the document is loaded, etc.). In HTML, event handlers are attached to elements via event handler attributes (the attributes beginning with 'on', as in 'onkeyup').
Some event handlers, when invoked, produce purely decorative effects such as highlighting an image or changing the color of an element's text. Other event handlers produce much more substantial effects, such as carrying out a calculation, providing important information to the user, or submitting a form. For event handlers that do more than just change the presentation of an element, content developers should do the following:
- Use application-level event triggers rather than user interaction-level triggers. In HTML, application-level event attributes are 'onfocus', 'onblur' (the opposite of 'onfocus'), and 'onselect'. Note that these attributes are designed to be device-independent, but are implemented as keyboard specific events in current browsers.
- Otherwise, if you must use device-dependent attributes, provide redundant input mechanisms (i.e., specify two handlers for the same element):
- Use 'onmousedown' with 'onkeydown'.
- Use 'onmouseup' with 'onkeyup'
- Although the WCAG1.0 states "Use 'onclick' with 'onkeypress'" we recommend not using onkeypress because it is badly supported in many browsers and may hijack functionality a user wants (for example in JAWS screen reader, Opera browser and others). More details on why to not use onkeypress. Also note that 'onclick' is actually device independent (for links and form controls) and will work with keyboard or mouse. Internet Explorer for Mac is a notable exception.
Note that there is no keyboard equivalent to double-clicking ('ondblclick') in HTML.
- Do not write event handlers that rely on mouse coordinates since this prevents device-independent input.
Back to top