👀 Live Preview
Type in the field — printable keys show below (legacy keypress demo):
Last keypress: (none)

The onkeypress attribute is an inline event handler for the keypress event — historically fired when the user presses a printable character key in a focused field. You will still see it in older tutorials and legacy code. Important: the keypress event is deprecated in modern web standards. For new projects, use onkeydown, onkeyup, or oninput instead. This page teaches onkeypress so you understand legacy markup, with clear guidance on what to use today.
Inline JS.
Character keys.
Use keydown.
When to use.
Not keyCode.
Filter input.
onkeypress AttributeThe original purpose of onkeypress was to react when a user typed a character — for example blocking non-alphanumeric input or counting keystrokes. It fires after keydown and before keyup for character keys in many browsers, but not for keys like Escape, arrows, or F-keys.
Today, the same goals are better achieved with oninput (value changed), pattern / inputmode HTML attributes, or onkeydown with event.key. Learn onkeypress to maintain old code; write new code with modern events.
The keypress event is deprecated. Browsers may still fire it for compatibility, but MDN and modern specs recommend keydown or input for new work.
Set onkeypress on a focusable element (legacy pattern):
<input type="text" onkeypress="myFunction(event)">
<script>
function myFunction(e) {
console.log("Character key:", e.key);
}
</script>keypress event fires.event and use event.key — avoid legacy keyCode.event.preventDefault() to block a character from entering the field.onkeydown or oninput.element.addEventListener("keypress", handler).keypress event bubbles.The onkeypress attribute accepts a string of JavaScript code:
onkeypress="myFunction()" — Call a named function.onkeypress="validateUsername(event)" — Pass the event object.field.onkeypress = (e) => { … } assigns the handler.// Preferred for new code — not keypress:
username.addEventListener("keydown", (e) => {
if (e.key.length === 1 && !/^[a-z0-9]$/i.test(e.key)) {
e.preventDefault();
}
});
// Or validate the whole value:
username.addEventListener("input", () => {
username.value = username.value.replace(/[^a-z0-9]/gi, "");
});| Action | keypress fires? | Modern alternative |
|---|---|---|
| Type letter or number | Often yes | onkeydown or oninput |
| Press Escape / Arrow keys | Usually no | onkeydown |
| Paste text | No | oninput |
| Block invalid character | Legacy pattern | pattern or oninput |
| New project in 2026 | Avoid | onkeydown |
| Handler attribute | onkeypress | Legacy inline handler |
| Target | Supported? | Notes |
|---|---|---|
<input type="text"> | Yes (legacy) | Common in old tutorials |
<textarea> | Yes (legacy) | Character input |
document via script | Yes (legacy) | Prefer keydown |
<div> without tabindex | No | Not focusable |
onkeypress vs onkeydown vs oninput| Handler | Status | Typical use today |
|---|---|---|
onkeypress | Deprecated | Read legacy code only |
onkeydown | Current | All keys, shortcuts |
oninput | Current | Value changed (incl. paste) |
Alphanumeric filter (legacy style), addEventListener, and the modern keydown replacement.
Type in the field — printable keys show below (legacy keypress demo):
Last keypress: (none)
Block non-alphanumeric characters with onkeypress and event.key:
<input type="text" id="username"
onkeypress="validateUsername(event)">
<script>
function validateUsername(e) {
if (e.key.length === 1 && !/^[a-z0-9]$/i.test(e.key)) {
e.preventDefault();
}
}
</script>preventDefault() stops the character from appearing. This replaces old keyCode checks with readable event.key tests.
Attach a legacy keypress listener in script:
<input type="text" id="dynamicField">
<script>
document.getElementById("dynamicField").addEventListener("keypress", function (e) {
document.getElementById("log").textContent = "Key: " + e.key;
});
</script>Same registration pattern as other events — only the event name is legacy.
Strip invalid characters after any change (including paste):
const username = document.getElementById("username");
username.addEventListener("input", () => {
username.value = username.value.replace(/[^a-z0-9]/gi, "");
});Validate the field value, not individual key codes. Simpler and more reliable for beginners.
required, pattern, and labels work with assistive tech.aria-invalid and aria-describedby on validated fields.input and keydown.Printable key.
Then keypress (legacy).
onkeypress runs.
Use keydown/input today.
Browsers still fire keypress for backward compatibility, but the event is deprecated. Do not build new features on onkeypress.
onkeypress supportWorks for many printable keys today, but specs and MDN recommend keydown or input instead.
Bottom line: Learn it for old code; use onkeydown or oninput for new projects.
onkeydown or oninput in new projectsevent.key instead of deprecated keyCodepattern and inputmode when possibleonkeypress when maintaining legacy scriptsonkeypresskeyCode / which in new codeThe onkeypress attribute handled printable character keys in older web pages. It remains useful to recognize in legacy code, but modern development should use onkeydown, onkeyup, or oninput instead.
When you see onkeypress in a tutorial or codebase, know what it does — then reach for up-to-date events when you write your own handlers.
onkeypressUnderstand legacy keyboard events — then use modern ones.
Avoid in new code.
StatusNot all keys.
ScopeModern fix.
ReplaceNot keyCode.
APIFull value.
Validatekeypress event fires — mainly for printable character keys in focused fields.onkeydown, onkeyup, or oninput for new projects.onkeydown fires for almost every key. onkeypress historically targeted character keys only and is deprecated.onkeydown with event.key === "Escape".event.key in modern code. keyCode is deprecated.Practice onkeypress in the editor, then compare with modern onkeydown and oninput patterns.
5 people found this page helpful