W3Schools.com

onkeypress Event

Event Object Reference Event Object

Example

Execute a JavaScript when a user is pressing a key or holding down a key:

<input type="text" onkeypress="return noNumbers(event)" />

Try it yourself »

Definition and Usage

The onkeypress event occurs when the user is pressing a key or holding down a key.

Tip: The order of events related to the onkeypress event:

  1. onkeydown
  2. onkeypress
  3. onkeyup

Note: The onkeypress event is not fired for all keys (e.g. ALT, CTRL, SHIFT, ESC) in all browsers. To detect only whether the user has pressed a key, use the onkeydown event instead, because it works for all keys.

Syntax

In HTML:

<element onkeypress="SomeJavaScriptCode">

In JavaScript:

object.onkeypress="SomeJavaScriptCode"

Parameter Description
SomeJavaScriptCode Required. Specifies a JavaScript to be executed when the event occurs


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The onkeypress event is supported in all major browsers.


onkeypress is Supported by the Following HTML Tags:

<a>, <acronym>, <address>, <area>, <b>, <bdo>, <big>, <blockquote>, <body>, <button>, <caption>, <cite>, <code>, <dd>, <del>, <dfn>, <div>, <dt>, <em>, <fieldset>, <form>, <h1> to <h6>, <hr>, <i>, <input>, <kbd>, <label>, <legend>, <li>, <map>, <object>, <ol>, <p>, <pre>, <q>, <samp>, <select>, <small>, <span>, <strong>, <sub>, <sup>, <table>, <tbody>, <td>, <textarea>, <tfoot>, <th>, <thead>, <tr>, <tt>, <ul>, <var>

onkeypress is Supported by the Following JavaScript Objects:

Document, Window


Event Object Reference Event Object
WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$7.95/mo SEO Hosting
Premium Website Design
WEB BUILDING
XML Editor - Free Trial!
FREE Website BUILDER
Free Website Templates Free CSS Templates
Make Your Own Website
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE