Java Reference
In-Depth Information
Next you have the radCPUSpeed_onclick() function, which is called by the onclick event handler
in each radio button. Your function has one parameter, namely the index position in the radCPUSpeed
collection of the radio object selected.
function radCPUSpeed_onclick(radIndex)
{
var returnValue = true;
The fi rst thing you do in the function is declare the returnValue variable and set it to true . You'll be
returning this as your return value from the function. In this case the return value is important because
it decides whether the radio button remains checked as a result of the user clicking it. If you return
false , that cancels the user's action, and the radio button remains unchecked. In fact no radio button
becomes checked, which is why you keep track of the index of the checked radio button so you can
track which button was the previously checked one. To allow the user's action to proceed, you return
true .
As an example of this in action, you have an if statement on the next line. If the radio button's index
value passed is 1 (that is, if the user checked the box for a 4.8 GHz processor), you tell the user that it's
out of stock and cancel the clicking action by setting returnValue to false.
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;
}
As previously mentioned, canceling the clicking action results in no radio buttons being checked. To
rectify this, you set the previously checked box to be checked again in the following line:
document.form1.radCPUSpeed[radCpuSpeedIndex].checked = true;
What you are doing here is using the collection for the radCpuSpeed radio group. Each element
in the collection actually contains an object, namely each of your three Radio objects. You use the
radCpuSpeedIndex variable as the index of the Radio object that was last checked, since this is what
it holds.
Finally, in the else statement, you set radCpuSpeedIndex to the new checked radio button's index
value.
else
{
radCpuSpeedIndex = radIndex;
}
In the last line of the function, the value of returnValue is returned to where the function was called
and will either cancel or allow the clicking action.
return returnValue;
}
Search WWH ::




Custom Search