Setting One Field with Another

With your forms, you’ll often find that if the user makes one choice, that choice dictates the value of other fields on the form. For example, let’s say that the sunroof option is only available on a two-door model. You could deal with this in two ways. First, you could check the entry and put up an alert dialog if the user makes the wrong choice. But it’s a slicker design to simply make the entry for the user. So if they pick the sunroof, the script automatically clicks the two-door button, as in Figure 7.10. Script 7.13 shows you how.

Figure 7.10. When the user checks the sunroof option, the script automatically sets the two-door radio button.

To set a field value automatically:

1.
document.getElementById("sunroof").onclick ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.