106Part IIJavaScript TutorialListing 9-4(continued) Choose a place to

106Part IIJavaScript TutorialListing 9-4(continued)

Choose a place to go:

Recent browsers also expose the valueproperty of the selected option item by way of thevalueproperty of the selectobject. This is certainly a logical and convenient shortcut, andyou can use it safely if your target browsers include IE, Mozilla-based browsers, and Safari. There is much more to the selectobject, including the ability to change the contents of a listin newer browsers. Chapter 24 covers the selectobject in depth. Passing Form Data and Elements to FunctionsIn all of the examples so far in this lesson, when an event handler invokes a function thatworks with form elements, the form or form control is explicitly referenced in the function. But valuable shortcuts exist for transferring information about the form or form controldirectly to the function without dealing with those typically long references that start withthe windowor documentobject level. JavaScript features a keyword this that always refers to whatever object contains thescript in which the keyword is used. Thus, in an onchangeevent handler for a text field, youcan pass a reference to the text object to the function by inserting the thiskeyword as aparameter to the function: At the receiving end, the function defines a parameter variable that turns that reference into avariable that the rest of the function can use: function upperMe(field) { statement[s] } The name you assign to the function s parameter variable is purely arbitrary, but it is helpfulto give it a name that expresses what the reference is. Importantly, this reference is a live connection back to the object. Therefore, statements in the script can get and set propertyvalues of the object at will. Note

Leave a Reply