<script language="javascript">
var rbobject= document.testform.rb1;
</script>
The object rbobject is an array of radio elements. To use the first radio button,
we have to call rbobject[0], for second box it is rbobject[1] and so on.
Here are the events, dom properties and method associated with
Radio Button element.
Event Handlers: Associated with Form type Radio Button:
All the examples below use a javascript function output
<script language=javascript>
function output()
{
alert("testing RadioButton events");
}
</script>
Event Handler
Description
Example
onMouseOver
This is invoked when
a mouse is moved over the Radio Button
This is invoked when
a mouse click is done on the radio
<input type=radio onClick="output()">
Result: CB1
onBlur
Executes some code when the Radio Button loses focus using tab
<input type=radio onBlur="output()">
Result: CB1
onFocus
Executes some code when the radio button gets the focus using tab
<input type=radio onFocus="output()">
Result: CB1
DOM Properties:
The following are the list of DOM (Dynamic Object Model) properties
that can be used to get and alter RadioButton properties in javascript.
The below examples are based on the form
<form name=testrb>
<input name=myrb1 type=radio value=xxx> Checking 1
<input name=myrb2 type=radio value=xxx> Checking 2
</form>
DOM Property
Description
Example
checked
Used to check or select RadioButton selection
To Check:
var ss = document.testb.myrb[0].checked;
To Select:
document.testb.myrb[1].checked = true;
This will select second element.
defaultChecked
Used to check whether the Radio Button is
checked by default
To Get:
var ss = document.testb.myrb[0].defaultChecked;
form
Used to get the parent node (form object)
of this RadioButton
To Get:
var ss = document.testb.myrb[0].form;
name
Used to get Radio Button name
To Get:
var ss = document.testb.myrb[0].name;
type
Used to get form type
To Get:
var ss = document.testb.myrb[0].type;
value
Used to set or get RadioButton value
To Get:
var ss = document.testb.myrb[0].value;
To Set::
document.testb.myrb[0].value = "testy";
DOM Methods:
The following are the list of DOM (Dynamic Object Model) methods
that can be used to do dynamic changes like dynamic Radiobutton selection using javascript.
DOM Method
Description
Example
click()
Used to dynamically make a Radio Button checked
To Click:
document.testb.myrb.click();
blur()
Used to dynamically make the Radio Button blur
To Blur:
document.testb.myrb.blur();
focus()
Used to dynamically get focus on the Radio Button
To Focus:
document.testb.myrb.focus();
Example: Making Radiobutton Select on Mouse Over
<script language=javascript>
function rbevent()
{
var xx = document.xx.rbtest;
xx.checked = true;
}
function rbeventq()
{
var xx = document.xx.rbtest;
xx.checked = false;
}
</script>
Javascript (JS) is one of the most used languages in the world. Some times spelled as Java Script.
Hope you enjoy this tutorial. Do send your feedback or suggestions on this javascript or java script tutorial. This is a copyright content.