Java Reference
In-Depth Information
it between the start and closing tags, rather than by using the
value
attribute. So if you want a
<textarea/>
element 40 characters wide by 20 rows deep with initial text
Hello World
on the first
line and
Line 2
on the second line, you define it as follows:
<textarea name="myTextArea" cols="40" rows="20">Hello World
Line 2
</textarea>
Another attribute of the
<textarea/>
element is the
wrap
attribute, which determines what happens
when the user types to the end of a line. The default value for this is
soft
, so the user does not have
to press Return at the end of a line, though this can vary from browser to browser. To turn wrapping
on, you can use one of two values:
soft
and
hard
. As far as client‐side processing goes, both do the
same thing: They switch wrapping on. However, when you come to server‐side processing, they do
make a difference in terms of which information is sent to the server when the form is posted.
If you set the
wrap
attribute on by setting it to
soft
, wrapping will occur on the client side, but
the carriage returns won't be posted to the server, just the text. If the
wrap
attribute is set to
hard
,
any carriage returns caused by wrapping will be converted to hard returns—it will be as if the
user had pressed the Enter key, and these returns will be sent to the server. Also, you need to be
aware that the carriage‐return character is determined by the operating system that the browser is
running on—for example, in Windows a carriage return is
\r\n
, on UNIX, UNIX‐like systems, and
Mac OS X, a carriage return is
\n
. To turn off wrapping client‐side, set
wrap
to
off
.
Note
The
\n
character is the universal line feed character. If you are
formatting raw text output and need a new line,
\n
works in every browser on
every operating system.
The object created by the
<textarea/>
element has the same properties, methods, and events as the
text box object you saw previously, except that the text area doesn't have the
maxlength
attribute.
Note that there is a
value
property even though the
<textarea/>
element does not have a
value
attribute. The
value
property simply returns the text between the
<textarea>
and
</textarea>
tags. The events supported by the
<textarea/>
element object include the
keydown
,
keypress
,
keyup
, and
change
event handlers.
event Watching
trY it out
To help demonstrate how the
keydown
,
keypress
,
keyup
, and
change
events work (in particular, the
order in which they fire), you'll create an example that tells you what events are firing:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chapter 11: Example 5</title>
</head>
<body>
<form action="" name="form1">