You are here: Reference > JavaScript > client-side > HTML DOM > objects > nodes and tags > input:radio

input:radio object

Browser support:
Creates a radio button control.
The radio button allows the user to choose one of a predefined set of options. You can define groups with the name property of the radio buttons.
  • Radio buttons with the same name belong to the same group.
  • Radio buttons with different names belong to different groups.
  • At most one radio button can be checked in a group.
If you need to use independent options, choose the input:checkbox element instead.
Using many items in the same radio group results in an incomprehensible user interface. Use the selection list element in that case. This control is one of the form controls.
The state of the control can be submitted to a server if the following conditions are met:
  • A form element must contain the radio button element.
  • The action property of the container form must be set to the URL of the server.
  • The name property of the radio button must be specified and non-empty.
If the control is in checked state when the container form is submitted, the name of the radio button is sent with the value of the value property. If the control is not checked, no information belonging to the radio button is sent with the form. The default value of the value property is 'on' for radio buttons, but the property is writable. Define different values for radio buttons in the same group, otherwise you cannot identify on the server side which one was checked.
You can access the state of the control with the checked property. Sometimes it is useful to check the state before submitting. See Example 2 for details.
If you need to detect when the selection has changed in a radio group, use the onclick event instead of onchange event, because the onchange event occurs when the radio button loses focus in Internet Explorer, not when the state has changed. See Example 3.

Syntax:

Methods that return the object:
var inputObj = document.createElement ("input");   inputObj.type = "radio"
The base interface, through which you can add new functionalities to the input:radio object, is the HTMLInputElement interface.
If you want to see the HTML objects by categories, please visit this page.
HTML page for this element: input:radio

Possible members:

Properties
Methods
Events
Style properties
accessKey
Sets or retrieves an access key to an element.
attributes
Represents a collection of attribute nodes that belong to an element.
baseURI
10
Returns the base URL for the object.
behaviorUrns
Represents a collection of the Uniform Resource Names for all behaviors attached to an element.
canHaveChildren
Retrieves a Boolean value that indicates whether the element can contain child elements.
canHaveHTML
Retrieves a Boolean value that indicates whether the element can contain HTML formatted text.
checked
Sets or retrieves the state of a check box or a radio button.
className
Sets or retrieves the style class or classes that belong to the element.
clientHeight
Returns the height of the visible area for an object, in pixels. The value contains the height with the padding, but it does not include the scrollBar, border, and the margin.
clientLeft
Returns the width of the left border in pixels.
clientTop
Returns the height of the top border in pixels.
clientWidth
Returns the width of the visible area for an object, in pixels. The value contains the width with the padding, but does not include the scrollBar, border, and the margin.
contentEditable
3
Sets or retrieves whether the contents of the object are editable.
currentStyle
Represents the computed style settings for an element.
defaultChecked
Specifies or returns the initial state of a input:checkbox or input:radio elements. The initial state can be set with the CHECKED attribute in HTML.
defaultValue
Specifies or returns the initial value of the object. The initial state can be set with the value attribute in HTML.
dir
Sets or retrieves the text direction as related to the lang property.
disabled
Sets or retrieves the state of an object for user interaction.
filters
Represents a collection of all filter objects applied to an element.
form
Returns a reference to the form element in which the object is placed.
forms
Represents a collection of all form elements in the current document.
hideFocus
Specifies or returns whether a dotted rectangle (focus rectangle) is drawn around an object while it has focus.
hspace
Specifies or returns the number of pixels to use as a margin at the left and right sides of the object.
id
Sets or retrieves a unique identifier for the object.
isContentEditable
Returns a Boolean value that indicates whether the contents of the object are editable by the user.
isDisabled
Returns a Boolean value that indicates whether the object is disabled.
isMultiLine
Returns a Boolean value that indicates whether the contents of an element can be multiline or not.
isTextEdit
Returns a Boolean value that indicates whether the createTextRange method can be used for the element.
lang
Specifies or returns the language of the element.
language
Sets or retrieves the scripting language for the current element. Use it only for the script element.
localName
9
Returns the local part of the qualified name of the current node.
name
Sets or retrieves the name of a form control that affects the contents of the message submitted to the server.
namespaceURI
93.6
Sets or returns the namespace URI of the current node.
nextElementSibling
93.5
Returns a reference to the next child element of the current element's parent.
nextSibling
Returns a reference to the next child of the current element's parent.
nodeName
Returns the name of the current node.
nodeType
Returns an integer that indicates the type of the node.
nodeValue
Sets or returns the value of the current node.
offsetHeight
Returns the height of the visible area for an object, in pixels. The value contains the height with the padding, scrollBar, and the border, but does not include the margin.
offsetLeft
Returns the left position of an object relative to the left side of its offsetParent element, in pixels.
offsetParent
Returns a reference to the closest ancestor element in the DOM hierarchy from which the position of the current element is calculated.
offsetTop
Returns the top position of the object relative to the top side of its offsetParent element, in pixels.
offsetWidth
Returns the width of the visible area for an object, in pixels. The value contains the width with the padding, scrollBar, and the border, but does not include the margin.
outerHTML
Sets or retrieves the outer HTML content (the source code including the opening and closing tags) of an element.
outerText
Sets or returns the text content of an element including the text content of its descendants.
ownerDocument
Returns the document object that contains the current node.
parentElement
Returns the parent element of the object in the DOM hierarchy.
parentNode
Returns the parent element of the current node in the DOM hierarchy.
parentTextEdit
Returns the closest ancestor element of the current element in the DOM hierarchy that can be used to create a TextRange object.
previousElementSibling
93.5
Returns a reference to the previous child element of the current element's parent.
previousSibling
Returns a reference to the previous node of the current element's parent.
readyState
Returns a string value that represents the state of the object.
recordNumber
Returns an integer value that indicates the ordinal position of the item within the data set that generated the current object.
runtimeStyle
Represents the overridden style settings for an element.
scopeName
Retrieves the local name of the namespace declared for the current element.
size
Specifies or returns the width of a control, in characters.
sourceIndex
Returns the position of the current object in the all collection of the document.
status
Returns a Boolean value that indicates whether the current element is selected.
style
Represents the inline style settings for an element or a CSS rule.
tabIndex
Specifies or returns the tabbing order for keyboard navigation using the TAB key.
tagName
Returns the tag name of the current element.
tagUrn
Sets or retrieves the Uniform Resource Name (URN) of the namespace declared for the current element.
title
Specifies or returns a tooltip for an element.
type
Sets or retrieves the type of the input element.
uniqueID
Returns the unique identifier generated by the browser for the object.
unselectable
Sets or retrieves whether the selection process can start in an element's content.
value
Specifies or returns the value of the control.
vspace
Specifies or returns the number of pixels to use as a margin at the top and bottom sides of an object.

Example HTML code 1:

This example illustrates the use of the radio button element:
<form method="post" action="#URL#">
    <input type="radio" name="sex" id="radio_female" value="female" checked="checked" /> 
    <label for="radio_female">female</label>
    <br />
    <input type="radio" name="sex" id="radio_male" value="male" /> 
    <label for="radio_male">male</label>
    <br /><br />
    <input type="submit" value="Register" />
</form>
Did you find this example helpful? yes no

Example HTML code 2:

This example checks the state of the radio button elements before submitting the form:
<head>
    <script type="text/javascript">
        function CheckAndSubmit () {
            var regForm = document.getElementById ("regForm");
            var sexRadios = document.getElementsByName ("sex");

            var selected = false;
            for (var i = 0; i < sexRadios.length; i++) {
                if (sexRadios[i].checked) {
                    selected = true;
                    break;
                }
            }
            if (!selected) {
                alert ("Please select your sex to register!");
                return;
            }

            regForm.submit ();
        }
    </script>
</head>
<body>
    <form id="regForm" method="post" action="#URL#">
        <input type="radio" name="sex" id="radio_female" value="female" /> 
        <label for="radio_female">female</label>
        <br />
        <input type="radio" name="sex" id="radio_male" value="male" /> 
        <label for="radio_male">male</label>
        <br /><br />
        <input type="button" value="Register" onclick="CheckAndSubmit ()" />
    </form>
</body>
Did you find this example helpful? yes no

Example HTML code 3:

This example shows how to detect when the selection has changed in a radio group:
<head>
    <script type="text/javascript">
        function OnChangeRadio (radio) {
            alert ("The " + radio.value + " radio is selected.");
        }
    </script>
</head>
<body>
    Select your sex:<br /><br />
    <input type="radio" name="sex" id="radio_female" value="female" onclick="OnChangeRadio (this)" /> 
    <label for="radio_female">female</label>
    <br />
    <input type="radio" name="sex" id="radio_male" value="male" onclick="OnChangeRadio (this)" /> 
    <label for="radio_male">male</label>
</body>
Did you find this example helpful? yes no

Example HTML code 4:

This example shows how to enable/disable an element when the selection has changed in a radio group:
<head>
    <script type="text/javascript">
        function OnChangeEyeColor () {
            var radio = document.getElementById ("radio_speceyes");
            var textBox = document.getElementById ("text_speceyes");
            textBox.disabled = !radio.checked;
        }
    </script>
</head>
<body onload="OnChangeEyeColor ()">
    Color of your eyes:<br /><br />
    <input type="radio" name="eyecolor" id="radio_blueeyes" value="blue" onclick="OnChangeEyeColor ()" checked="checked" />
    <label for="radio_blueeyes">blue</label>
    <br />
    <input type="radio" name="eyecolor" id="radio_browneyes" value="brown" onclick="OnChangeEyeColor ()" />
    <label for="radio_browneyes">brown</label>
    <br />
    <input type="radio" name="eyecolor" id="radio_speceyes" onclick="OnChangeEyeColor ()"/>
    <label for="radio_speceyes">other:</label>
    <input type="text" id="text_speceyes" value="black"/>
</body>
Did you find this example helpful? yes no

Related pages:

External links:

User Contributed Comments

Post Content

Post Content