Car Picker Radio Buttons

Email Address:
Colors:
Power Windows
Doors: Two Four
Enter your Zip code or pick the dealer nearest you:

Zip:

Radio Button Checking with JavaScript

This radio button checking uses a loop and a test within the loop.

The most difficult part about this JavaScript code may be figuring out why JavaScript recognizes the property "checked" for DoorCt. "Checked" is not a JavaScript reserved word or property. In fact, "checked" is a property (Attribute) of an HTML <INPUT> This shows the close relationship between JavaScript and HTML. Javascript recognizes the properties of HTML tags, and you can use them in JavaScript coding.

In this instance, the JavaScript checks to see whether one of the INPUT tags named DoorCT has been checked.

The power of an HTML form or other HTML code using JavaScript is harnessed in most cases by passing information back and forth to a common variable from the body of the HTML web page to a JavaScript processing script, often located in the HTML HEAD:

  1. Define a function or an object (variable):
  2. function
  3. Object

Radio buttons are grouped together so that only one radio button can be checked. Radio buttons in the same group are given the same Variable Name using the INPUT tag's Name attribute. The most common radio button format is:

The above button is checked by default, and sends data to the CGI server if checked. Also, the browser program only permits one radio button in a group to be checked.

What's left for JavaScript to do? JavaScript is used to check to see if any of the radio buttons in a group are checked. If your form requires that a button in the group be selected by the user, then this JavaScript is good to use.

But why do this, if you can check one button by default using the standard HTML code? The answer lies in avoiding state "negative option" laws. "Negative options" require a consumer to deselect an item the seller has selected for him/her; otherwise, the consumer is deemed to have purchased the item unless she takes action to decline the selection. This "negative option" scenario is unlawful in most states.

So, if you want to conform to the "negative option" laws but still have a consumer select a radio button item for which the user will pay, you should:

  1. Not set use the HTML "Checked" attribute;
  2. Use the following JavaScript code to prompt the user to check one of the buttonsm. This script requires the user to check something in order to complete the order form. That's OK -- that's not a negative option.