|
Week 14 THE PRODUCTShared Files Archive Final Plans Week 13 Animation LabDHTML Animation Week 12 DHTML Layers LabStretchtext Demo Week 11 DHTMLReplacement Lab Week 10 Progress ReportsStyle Sheets Style Sheets Lab Week 9 Maintaining StateCookies Invisible Forms State-Keeping Lab Week 8 Preloading ImagesJavaScript and Forms Assignment 3 Assignment 2 Work Prototype Logos Week 7 Barlow's DeclarationRandom Content Week 6 Name RevealedGround Rules 2 Revised Syllabus Week 5 Assignments for 10/7Week 4 Window Control DemoProposed Names Ground Rules Advice on Research Week 3 Group AssignmentsAssignment 1 Work Javascript, Buttons, Frames Assignment 2 Week 2 Lecture 9/9Platform Detection JavaScript Basics Week 1 Class ListWeek 1 Checklist JavaScript Content Assignment 1 Server Access Publication Research Topics Syllabus Requirements Course Concept |
JavaScript and FormsI. Input and Output with FormsBecause all elements in HTML forms are exposed to JavaScript, you can use scripts to pass information to and from these elements, often without reloading the page. In many cases this is simply a matter of getting or setting the form element's value property, as in the example below: After you've been warned about a null input condition, try typing something into the text box before you click the button. The script associated with this form (triggered by an onClick handler within the Click Me button's <INPUT> tag) performs both read and write operations on the single text input of this form. To read the current contents of that text box, we use a statement like this:
someVariable = document.forms[0].elements[0].value;
As you can see from the familiar dot notation, JavaScript-enabled browsers generate a forms array with an entry for every form on the page, much like the images array. Unlike images, however, the forms array contains subsidiary levels of information: it has an array of elements in which each component may have several properties. In this case we access the value property of the first entry in the elements array of the first form on the page. Every form element, whether an <INPUT></INPUT> container, a <TEXTAREA></TEXTAREA> container, or a <SELECT></SELECT> container, has a numbered entry in the elements array for its form. As in the case of images, numbers are assigned according to the form or element's appearance in the markup. The first form defined on the page is document.forms[0], the second is document.forms[1], etc., and the same logic applies to elements within the forms. If we assign the current value of a text input to a variable, that variable automatically becomes a string variable. All strings have a length property, which lets us determine how many characters were entered. To change the value or some other attribute of a form element, we simply perform an assignment using a statement like this:
document.forms[0].elements[0].value = "boo";
The script blanks out the input box after 2 seconds by setting its value to "". (The delay is accomplished by a setTimeout command; view source for the details.) II. Processing Selection ListsSelection elements--sets of options deployed within <SELECT></SELECT> containers--need finer treatment in JavaScript, mainly because they are inherently more complicated. Here's an example of a selection element, or pull-down menu, with a simple script attached as in the first example, via a button element. Click the button to see your selection from the menu in the text box at right. |
||
|