GlobalEventHandlers.onkeypress

  • Revision slug: DOM/element.onkeypress
  • Revision title: element.onkeypress
  • Revision id: 31724
  • Created:
  • Creator: ziyunfei
  • Is current revision? No
  • Comment 35 words added, 21 words removed

Revision Content

{{ DomRef() }}

Summary

The onkeypress property sets and returns the onKeyPress event handler code for the current element.

Syntax

element.onkeypress = event handling code

Notes

The keypress event is raised when the user presses a key on the keyboard.

Specification

Not part of specification.

Example

The following example shows the use of the onkeypress event during a digitation into a form field:

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Example</title>
<script type="text/javascript">
  function numbersOnly(oToCheckField, oKeyEvent) {
    return oKeyEvent.charCode === 0 || /\d/.test(String.fromCharCode(oKeyEvent.charCode));
  }
</script>
</head>

<body>
<form name="myForm">
<p>Enter numbers only: <input type="text" name="myInput" onkeypress="return numbersOnly(this, event);" onpaste="return false;" /></p>
</form>
</body>
</html>

{{ languages( {"fr": "fr/DOM/element.onkeypress","zh-cn": "zh-cn/DOM/element.onkeypress" } ) }}

Revision Source

<p>{{ DomRef() }}</p>
<h3 id="Summary">Summary</h3>
<p>The <strong>onkeypress</strong> property sets and returns the onKeyPress event handler code for the current element.</p>
<h3 id="Syntax">Syntax</h3>
<pre class="eval">element.onkeypress = <em>event handling code</em>
</pre>
<h3 id="Notes">Notes</h3>
<p>The keypress event is raised when the user presses a key on the keyboard.</p>
<h3 id="Specification">Specification</h3>
<p>Not part of specification.</p>
<h3 id="Example">Example</h3>
<p>The following example shows the use of the <code>onkeypress</code> event during a digitation into a form field:</p>
<pre class="brush: html">&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Example&lt;/title&gt;
&lt;script type="text/javascript"&gt;
  function numbersOnly(oToCheckField, oKeyEvent) {
    return oKeyEvent.charCode === 0 || /\d/.test(String.fromCharCode(oKeyEvent.charCode));
  }
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;form name="myForm"&gt;
&lt;p&gt;Enter numbers only: &lt;input type="text" name="myInput" onkeypress="return numbersOnly(this, event);" onpaste="return false;" /&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>{{ languages( {"fr": "fr/DOM/element.onkeypress","zh-cn": "zh-cn/DOM/element.onkeypress" } ) }}</p>
Revert to this revision