w3resource logo

JavaScript Textarea Object - onkeypress Event handler

JavaScript Textarea Object : onkeypress Event handler

Secondary Nav


The onkeypress event handler of textarea object is defined in an <textarea> and </textarea> (i.e. <textarea name="mytextarea" onkeypress = "JavascriptCode"> </textarea>). It executes some JavaScript code or function when a key is pressed and released over the textarea object.


Implemented in JavaScript 1.2


onkeypress = "JavascriptCode" 


JavascriptCode : JavaScript statement or JavaScript function.

Example of Textarea object : onkeypress event handler

In the following web document onkeypress event handler shows the effect when a key is pressed and released over the textarea object.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript Textarea object's Event handler : onkeypress Example</title>
<script type="text/javascript">
function textareakeypress()
document.form1.messg.value = ctr++;
<h1 style="color: red">JavaScript Textarea object's Event handlers : onkeypress </h1>
<h3>Goto the text area by pressing tab key or mouse clicking and onkeypress  event will work and count
number of key is pressed and released in the following text box.</h3>
<hr />
<p>Input your details : </p>
<form name="form1" action="#">
<textarea name="TextArea" rows="5" cols="50" onkeypress='textareakeypress()'>
<br />
<p>Number of characters input : </p>
<input type="text" name="messg" size="50" />

View the example of textarea object's onkeypress event handler in the browser

Supported Browser

Internet Explorer 7 Firefox 3.6 Google Chrome 7 Safari 5.0.1 Opera 10
Yes Yes Yes Yes Yes

See also :

Javascript event handlers.

Javascript object's event handlers.

Is this content useful for you?


You might be using Adblocker. Since w3resource does not charge anything from users, all of our expenses (e.g. Hosting, Content Creation, Development etc.) are met by advertisement. Please whitelist w3resource.