Sams Teach Yourself HTML, CSS & JavaScript Web Publishing in One Hour a Day

(singke) #1
ptg16476052

Validating Forms with JavaScript 533

19


The form indicates that the Operating System field is also required, but because it’s
impossible to not select an item in that list, there’s no need to validate it.


You can use the JavaScript debugger built in to Chrome (or whichever browser you’re
using) to inspect this function further. In Chrome, open the Developer Tools and click
on the Sources button. Then click the button on the upper left, just below the toolbar,
to view a list of the scripts on the page. From there, select form.js from the list. You
should see a view like the one in Figure 19.2.


In the middle pane, you can see the source code for the script. Each line of the script is
numbered. If you click the line number for line 3, an arrow will appear. This sets what’s
called a breakpoint. Before the browser executes this line of the script, it will stop execu-
tion and let you inspect the inner workings of the script as it runs. This is incredibly use-
ful when you’re looking into problems in your scripts. You can see which variables are
set and what their values are, and you can control execution of the script from that point
on, resuming execution or proceeding one line at a time. After you’ve set the breakpoint
on line 3, click the Submit button on the form. The Developer Tools window will be
updated to look as it does in Figure 19.3.


FIGURE 19.2
The Chrome
Developer Tool
Sources view.


FIGURE 19.3
Running a script in
the debugger.



Free download pdf