JavaScript » Objects » Radio

The Radio object represents a single radio button, created by an HTML <INPUT> tag of type "radio", in a series from which the user may select only one. It is for this reason that all radio buttons in a group must have the same value for the NAME attribute. The Radio object is a form element and as such must be included within a <FORM> tag. The JavaScript runtime engine creates a Radio object for each individual radio button on the form. Those which form a group, and hence share the same NAME attribute, are stored as an array of that name. This array is in turn stored as a single element of the elements array of the Form object. You can access a set of radio buttons through this elements array either by the index number or by name. To access an individual button you need to refer to it as an element of the array of those buttons with the same NAME attribute.



For instance, assuming a set of radio buttons with the name "drink", you could refer to it as in this example.


...or by its number in the elements array of the appropriate form (assume it to be 3 in this case).


Then, say, to display the value of the radio button at element # 1 of the "drink" array, you could use something like this.



Syntax: object.checked

This property, which is by default tainted, is a Boolean value which reflects whether a particular radio button has been selected, returning true if it has and false if not. As only one radio button in a set can be selected at any one time, it follows that if the select property of one is true, then that property for the others of that set is false. The checked property can be set at any time and the change is immediately reflected in the display.


Syntax: object.defaultChecked

This property, which is by default tainted, is a Boolean value initially reflecting whether a particular radio button was selected by default using the CHECKED attribute, returning true if it was, and false if not. The defaultChecked property can be set at any time, but the change is not displayed, nor does it affect the defaultChecked property of any other radio button in the set.


Syntax: object.form

This property is a reference to the parent form of a set of radio buttons that share the same NAME attribute.



This property, tainted by default, refers to the NAME attribute of the set to which one particular radio button belongs. The name property can be set at any time but doing so places a radio button in a different group.


Syntax: object.type

This property specifies the type of an element in a form and reflects the TYPE attribute. In the case of a set of radio buttons, this is "radio".


Syntax: object.value

It is this property that is returned to the server when a radio button is selected and the form submitted. It is not displayed and so is not necessarily the same as any text that may appear alongside the radio button. The value property is tainted by default and reflects the VALUE attribute of the HTML code. Where no value is specified, the value property is the string "on".



Syntax: object.blur()

This method removes focus from a selection list.



This method programatically triggers a radio buttons onClick event handler.


Syntax: object.focus()

This method moves the focus to the specified selection list allowing the user to then select from it.


Syntax: object.handleEvent(event)

This method calls the handler for a specified event.

See Also: