Output via Text Boxes - Lecture Slides | CS 1030, Study notes of Computer Science

Material Type: Notes; Professor: Duhadway; Class: Foundations of Computer Science; Subject: Computer Science; University: Utah State University; Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-g7t-1
koofers-user-g7t-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2/24/2009
1
Chapter 7 - continued
Event-Driven Pages
Output via Text Boxes
a button provides a simple mechanism for user
interaction in a Web page
by clicking the button, the user initiates some action
a
text box
is an event-handler that can display
text (a word or phrase)
unlike an alert window, the text box appears as a box
embedded in the page
text can be written to the box by JavaScript code
(i.e., the box displays output)
Output via Text Boxes
for example, we could re-
implement the lucky
number page using a text
box
the text box containing
the random number is
embedded in the page
doesn't require the user
to close the alert window
after each number
Output via Text Boxes
general form of a text box element:
<input type="text" id="BOX_NAME"
size="NUM_CHARS" value="INITIAL_TEXT" />
the TYPE attribute of the INPUT element identifies the element
to be a text box
the ID attribute gives the element an identifier so that it can be
referenced (sometimes referred to as the name of the element)
the SIZE attribute specifies the size of the box (number of
characters that fit)
the VALUE attribute specifies text that initially appears in the
box
Output via Text Boxes
to display text in a text box, a JavaScript assignment
is used to assign to its value attribute
as part of the assignment, must specify the absolute
name of the box
the general form is:
document.getElementById('BOX_NAME').value =
VALUE_TO_BE_DISPLAYED;
when the button is
clicked, the JavaScript
commands are
executed
the commands
generate a random
number and assign it to
the text box
as a result, each
button click yields a
new number in the box
pf3

Partial preview of the text

Download Output via Text Boxes - Lecture Slides | CS 1030 and more Study notes Computer Science in PDF only on Docsity!

Chapter 7 - continued

Event-Driven Pages

Output via Text Boxes

 a button provides a simple mechanism for user

interaction in a Web page

 by clicking the button, the user initiates some action

 a text box is an event-handler that can display

text (a word or phrase)

 unlike an alert window, the text box appears as a box embedded in the page  text can be written to the box by JavaScript code (i.e., the box displays output)

Output via Text Boxes

 for example, we could re- implement the lucky number page using a text box  the text box containing the random number is embedded in the page  doesn't require the user to close the alert window after each number

Output via Text Boxes

general form of a text box element:

 the TYPE attribute of the INPUT element identifies the element to be a text box  the ID attribute gives the element an identifier so that it can be referenced (sometimes referred to as the name of the element)  the SIZE attribute specifies the size of the box (number of characters that fit)  the VALUE attribute specifies text that initially appears in the box

Output via Text Boxes

• to display text in a text box, a JavaScript assignment

is used to assign to its value attribute

• as part of the assignment, must specify the absolute

name of the box

• the general form is:

document.getElementById('BOX_NAME').value =

VALUE_TO_BE_DISPLAYED;

when the button is clicked, the JavaScript commands are executed

the commands generate a random number and assign it to the text box

as a result, each button click yields a new number in the box

Input via Text Boxes

 text boxes can also be used for receiving user input

 the user can enter text directly into the box  that text can then be accessed by JavaScript code via the absolute name of the box

document.geElementById('BOX_NAME').value

 note that the value retrieved from a text box is always a string  if the user enters a number, say 93, then the absolute name will access "93"  similar to prompt, you must use parseFloat to convert the string to its numeric value

Input via Text Boxes

 example: we can create a temperature conversion

page

 the user enters the Fahrenheit temperature in a text box  at the click of a button, the input is accessed and converted to Celsius  another text box is used to display the converted temperature

fahrBox is used for input

the button's onclick attribute specifies the code for converting the temperature

celsiusBox is used for output

Input and Output

note: the same text box can be used for both input and output  can modify the conversion page to allow for entering a temperature in either box, then convert to the other

Input and Output

 can write a simple page in which the user can enter a number, then double it by clicking a button

Text Areas

a text area is similar to a text box but it can contain any number of text lines general form of a text area element:

 the ID attribute gives the element a name so that it can be referenced  the ROWS attribute specifies the height (number of text lines) of the area  the COLS attribute specifies the width (number of characters) of the area  the WRAP attribute ensures that the text will wrap from one line to the next instead of running off the edge of the text area