JavaScript Events (Cont.)
onFocus, onBlur, and onChange
The onFocus
, onBlur
, and onChange
events are often used in combination with validation of form fields.
Below is an example of how to use the onChange
event, which executes JavaScript code when a change event occurs; that is, when a select
, text
, or textarea
field loses focus and its value has been modified.
The
lowerCase()
function will be called whenever the user changes the content of the field.
The function converts the uppercase letters in the Unix shell command to lowercase letters before the command is executed:
- The keyword
this
refers to the current object.
In general, in a method this
refers to the calling object.
- The method
getElementById()
of the object document
returns the element with the specified ID.
- The method
toLowerCase()
of the object string
returns the calling string value converted to lowercase.
Demonstration
The following demonstration shows how the script of HTML and JavaScript is displayed on the Web.