536 Part III . Document Objects Reference Form (Ftp web hosting)

536 Part III . Document Objects Reference Form element arrays Starting with NN2 and IE4, document object models provide a feature that is beneficial to a lot of scripters. If you create a series of like-named objects, they automatically become an array of objects accessible via array syntax (see Chapter 7). This is particularly helpful when you create forms with columns and rows of fields, such as in an order form. By assigning the same name to all fields in a column, you can employ for loops to cycle through each row using the loop index as an array index. As an example, the following code shows a typical function that calculates the total for an order form row (and calls another custom function to format the value): function extendRows(form) { for (var i = 0; i < Qty.length; i++) { var rowSum = form.Qty[i].value * form.Price[i].value form.Total[i].value = formatNum(rowSum,2) } } All fields in the Qty column are named Qty. The item in the first row has an array index value of zero and is addressed as form.Qty[i]. Unfortunately, Internet Explorer 3 does not turn like-named fields into an array of references. But you can still script repetitive moves through an organized set of fields. The key is to assign names to the fields that include their index numbers: Qty0, Qty1, Qty2, and so on. You can even assign these names in a for loop that generates the table: for (var i = 0; i <= rowcount; i++) { ... document.write( ) … } Later, when it comes time to work with the fields, you can use the indexing scheme to address the fields: for (var i = 0; i < Qty.length; i++) { var rowSum = form.elements[ Qty + i].value * form.elements[ Price + i].value form[ Total + i].value = formatNum(rowSum,2) } In other words, construct names for each item, and use those names as array index names. This solution is backward- and forward-compatible. About < INPUT > element objects While this chapter focuses strictly on the FORM element as a container of controls, the next three chapters discuss different types of controls that nest inside a form. Many of these controls share the same HTML tag: . Only the TYPE attribute of the tag determines whether the browser shows you a clickable button, a checkbox, a text field, or so on. The fact that one element has so many guises makes the system seem illogical at times to scripters. An INPUT element has some attributes (and corresponding scriptable object properties) that simply don t apply to every type of form control. For example, FORM
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Leave a Reply