Java Reference
In-Depth Information
For determining whether a user has actually checked or unchecked a check box, you need to use the
checked property of the Checkbox object. This property returns true if the check box is currently
checked and false if not.
Radio buttons are slightly different. Because radio buttons with the same name are grouped together,
you need to test each Radio object in the group in turn to see if it has been checked. Only one of the
radio buttons in a group can be checked, so if you check another one in the group, the previously
checked one will become unchecked, and the new one will be checked in its place.
Both Checkbox and Radio have the event handlers onclick, onfocus, and onblur, and these operate
as you saw for the other elements, although they can also be used to cancel the default action, such as
clicking the check box or radio button.
Try It Out Check Boxes and Radio Buttons
Let's look at an example that makes use of all the properties, methods, and events we have just dis-
cussed. The example is a simple form that enables a user to build a computer system. Perhaps it could
be used in an e-commerce situation, to sell computers with the exact specifi cations determined by the
customer.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Chapter 7: Example 6</title>
<script type=”text/javascript”>
var radCpuSpeedIndex = 0;
function radCPUSpeed_onclick(radIndex)
{
var returnValue = true;
if (radIndex == 1)
{
returnValue = false;
alert(“Sorry that processor speed is currently unavailable”);
// Next line works around a bug in IE that doesn't cancel the
// Default action properly
document.form1.radCPUSpeed[radCpuSpeedIndex].checked = true;
}
else
{
radCpuSpeedIndex = radIndex;
}
return returnValue;
}
function btnCheck_onclick()
{
var controlIndex;
var element;
var numberOfControls = document.form1.length;
var compSpec = “Your chosen processor speed is “;
Search WWH ::




Custom Search