Forms and JavaScript
Once you have a form on your web page, you can use JavaScript to read information from that form and display information in it. The mortgage monthly payment calculator, for example, reads the principal, interest rate, and other information the user types into the form, calculates a monthly payment based on this information, and then writes the result to the form.
Naming Form Elements
Before you can read from or write to an element of your form, you need to tell JavaScript which form element you're talking about by naming your form and its elements. The code in Figure 7-12 demonstrates how to name forms (❶) and their elements (❷ and ❸). Notice that you can't name the <option>
tag (❹). Figure 7-13 shows the simple form this code ...
Get The Book of JavaScript, 2nd 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.