Chapter 7

  1. If using the DOM Level 0 events, returning false from the event handler and the event-handler script cancels the submittal. If using DOM Level 2, set cancelBubble to true for IE, and call the preventDefault for other browsers, both based on the event object.

  2. The blur event is triggered when the field loses focus. This is a good time to check the text field to make sure it has valid data.

  3. The select options are stored in an array called Options. As such, you can add new options as you would add new array elements, making sure that the entry is a new Option object:

  4. opts[opts.length] = new Option("Option Four", "Opt 4");
  5. Here’s one approach:

  6. var rgEx = /^[A-Za-z\s]*$/g;
    var OK = rgEx.exec(document.someForm.text1.value);
  7. The code must first assign an event-handler function to each radio button’s onclick event handler:

  8. document.someForm.radiogroup[0].onclick=handleClick;
    document.someForm.radiogroup[1].onclick=handleClick;
  9. If there are several buttons, this can be managed in a for loop. In the handleClick function, test the check status, and disable the form element accordingly. For instance, to disable the submit button:

  10. function handleClick(  ) {
       if (document.someForm.radiogroup[1].checked) {
         document.someForm.submit.disabled=true;
       } else {
         document.someForm.submit.disabled=false;
      }
    }

Get Learning JavaScript 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.