You are here: Reference > JavaScript > client-side > event handling > properties > charCode (event)

charCode property (event)

Browser support:
9
Retrieves the Unicode character code of the key that generated the onkeypress event.
  • In Firefox, if the pressed key generates a character, the charCode property contains the character code, else the keyCode property.
  • In Google Chrome, Safari and Internet Explorer from version 9, the charCode and keyCode properties are identical for onkeypress events.
  • In Internet Explorer before version 9 and Opera, use the keyCode property instead.
  • In Firefox, Opera, Google Chrome, Safari and Internet Explorer from version 9, the which property can also be used, it always returns the Unicode character code of the key that generates the onkeypress event.

Syntax:

object.charCode;
You can find the related objects in the Supported by objects section below.
This property is read-only.

Possible values:

Integer that represents the Unicode character code.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the charCode property:
<head>
    <script type="text/javascript">
        function GetChar (event){
            var chCode = ('charCode' in event) ? event.charCode : event.keyCode;
            alert ("The Unicode character code is: " + chCode);
        }
    </script>
</head>
<body>
    <input size="40" value="Write a character into this field!" onkeypress="GetChar (event);"/>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example shows how to create a text field that does not accept numeric input:
<head>
    <script type="text/javascript">
        function FilterInput (event) {
            var chCode = ('charCode' in event) ? event.charCode : event.keyCode;

                // returns false if a numeric character has been entered
            return (chCode < 48 /* '0' */ || chCode > 57 /* '9' */);
        }
    </script>
</head>
<body>
    The following text field does not accept numeric input:
    <input type="text" onkeypress="return FilterInput (event)" />
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content