HTML and CSS Reference
In-Depth Information
Table 13.7
Form Event Handlers
Event Handler
When It Is Triggered
onBlur
When a form's select, text, or textarea field loses focus.
onChange
When a select, text, or textarea field loses focus and its value has been
changed.
onClick
When an object on a form is clicked.
onFocus
When a field receives input focus by tabbing with the keyboard or clicking
with the mouse in the field.
onReset
When the user resets the form.
onSelect
When a user selects some of the text within a text or textarea field.
onSubmit
When a user submits a form.
The
this
keyword refers to the current object and is especially helpful when dealing with
forms. In forms that contain multiple items, such as checkboxes, radio buttons, and
textboxes, it is easier to refer to the item with the
this
keyword than by using its full
name when calling a function or an event handler. (Examples of the
this
keyword are
shown in Chapter 11.)
In a form,
this
could be the form itself or one of the input devices. With an event han-
dler, the
this
keyword by itself references the current object, such as an input device,
whereas
this.form
references the form object where the input device was created.
EXAMPLE
13.13
<html>
<head><title>The this keyword</title>
<script type="text/javascript">
1
function display_formval(
myform
){
alert("text box value is: " +
myform.namestring.value
);
}
2
function display_buttonval(
mybutton
){
alert("button value is: " +
mybutton.value
);
}
</script>
</head>
<body><b>
<hr>
3
<form
name="simple_form">
<p>
Type your name here:
<input type="text"
name="namestring"
size="50" />