Web design conference - 681Chapter 23Text-Related Form ObjectstypeValue:String (text).Read-OnlyCompatibility:WinIE4+, MacIE4+, NN3+, Moz1+,

681Chapter 23Text-Related Form ObjectstypeValue:String (text).Read-OnlyCompatibility:WinIE4+, MacIE4+, NN3+, Moz1+, Safari1+ Use the typeproperty to help you identify a text input object from an unknown group of formelements. Related Items:form.elementsproperty. valueValue:String.Read/WriteCompatibility:WinIE3+, MacIE3+, NN2+, Moz1+, Safari1+ A text object s valueproperty is the two-way gateway to the content of the field. A referenceto an object s value property returns the string currently showing in the field. Note that allvalues coming from a text object are string values. If your field prompts a user to enter a num- ber, your script may have to perform data conversion to the number-as-string value ( 42 instead of plain, old 42) before a script can perform math operations on it. JavaScript tries tobe as automatic about this data conversion as possible and follows some rules about it (seeChapter 27). If you see an error message that says a value is not a number (for a math opera- tion), the value is still a string. Your script places text of its own into a field for display to the user by assigning a string tothe valueproperty of a text object. Use the simple assignment operator. For example: document.forms[0].ZIP.value = 90210 ; JavaScript is more forgiving about data types when assigning values to a text object. JavaScript does its best to convert a value to a string on its way to a text object display. EvenBoolean values get converted to their string equivalents trueor false. Scripts can placenumeric values into fields without a hitch. But remember that if a script later retrieves thesevalues from the text object, they will come back as strings. About the only values that don tget converted are objects. They typically show up in text boxes as [object]or, in somebrowsers, a more descriptive label for the object. Storing arrays in a field requires special processing. You need to use the array.join() method to convert an array into a string. Each array entry is delimited by a character youestablish in the array.join()method. Later you can use the string.split()method toturn this delimited string into an array. As a demonstration of how to retrieve and assign values to a text object, Listing 23-2 showshow the action in an onchangeevent handler is triggered. Enter any lowercase letters intothe field and click out of the field. I pass a reference to the entire form object as a parameterto the event handler. The function extracts the value, converts it to uppercase (using one ofthe JavaScript string object methods), and assigns it back to the same field in that form. Listing 23-2:Getting and Setting a Text Object s Value