Enter the name of your favorite Beatles song:
Chapter Forms and Form Elements Get to know the usage of the this keyword in passing form and form element objects to functions The technique not only saves you typing in your code, but assures accuracy in references to those objects Submitting Forms If you have worked with Web pages and forms before, you are familiar with how simple it is to add a Submit-style button that sends the form to your server However, design goals for your page may rule out the use of ugly system buttons If you’d rather display a pretty image, the link tag surrounding that image should use the javascript: URL technique to invoke a script that submits the form The scripted equivalent of submitting a form is the form object’s submit() method All you need in the statement is a reference to the form and this method, as in document.forms[0].submit() One limitation might inhibit your plans to secretly have a script send you an email message from every visitor who comes to your Web site If the form’s ACTION attribute is set to a mailTo: URL, JavaScript will not pass along the submit() method to the form Of course, Internet Explorer does not allow e-mailing of forms through any machinations Before a form is submitted, you may wish to perform some last-second validation of data in the form or other scripting (for example, changing the form’s action property based on user choices) This can be done in a function invoked by the form’s onSubmit= event handler Specific validation routines are beyond the scope of this tutorial (although explained in substantial detail in Chapter 37), but I want to show you how the onSubmit= event handler works In all but the first generation of scriptable browsers from Microsoft and Netscape, you can let the results of a validation function cancel a submission if the validation shows some data to be incorrect or a field is empty To control submission, the onSubmit= event handler must evaluate to return true (to allow submission to continue) or return false (to cancel submission) This is a bit tricky at first, because it involves more than just having the function called by the event handler return true or false The return keyword must also be part of the final evaluation Listing 9-6 shows a page with a simple validation routine that ensures all fields have something in them before allowing submission to continue Notice how the onSubmit= event handler (which passes the form object as a parameter — in this case the this keyword points to the form object) includes the return keyword before the function name When the function returns its true or false value, the event handler evaluates to the requisite return true or return false Listing 9-6: Last-minute Checking before Form Submission Validator (continued) 11 12 Part II JavaScript Tutorial Listing 9-6 Continued function checkForm(form) { for (var i = 0; i < form.elements.length; i++) { if (form.elements[i].value == "") { alert("Fill out ALL fields.") return false } } return true } Please enter all requested information: First Name: Last Name: Rank: Serial Number: One quirky bit of behavior involving the submit() method and onSubmit= event handler needs explanation While you might think (and logically so, in my opinion) that the submit() method would be exactly the scripted equivalent of a click of a real Submit button, it’s not In Navigator, the submit() method does not cause the form’s onSubmit= event handler to fire at all If you want to perform validation on a form submitted via the submit() method, invoke the validation in the script that calls the submit() method So much for the basics of forms and form elements In the next lesson, you step away from HTML for a moment to look at more advanced JavaScript core language items: strings, math, and dates Exercises Rework Listings 9-1, 9-2, 9-3, and 9-4 so that the script functions all receive the most efficient form or form element references from the invoking event handler Modify Listing 9-6 so that instead of the submission being made by a Submit button, the submission is performed from a hyperlink Be sure to include the form validation in the process Chapter Forms and Form Elements In the following HTML tag, what kind of information you think is being passed with the event handler? Write a function that displays in an alert dialog box the information being passed A document contains two forms, named specifications and accessories In the accessories form is a field named acc1 Write two different statements that set the contents of that field to Leather Carrying Case Create a page that includes a select object to change the background color of the current page The property that needs to be set is document.bgColor, and the three values you should offer as options are red, yellow, and green In the select object, the colors should be displayed as Stop, Caution, and Go Note: If you are using a Macintosh or UNIX version of Navigator, you must be using Version or later for this exercise 3 13 ... string I will come back to forms later in this chapter to show you how to submit a form without a Submit button and how client-side form validation works Chapter Forms and Form Elements Text Objects... This may require conversion to numbers (Chapter 6) if text fields are used to enter values for some math operations Text object behavior Many scripters look to JavaScript to solve what are perceived... Chapter Forms and Form Elements There is much more to the select object, including the ability to change the contents of a list in newer browsers Chapter 24 covers the object