Putting Information into Forms

 

Introduction.  So now that we have talked about obtaining information from the controls on forms we also need to develop approaches to using JavaScript to put information into the controls on the form from the code.  As you'd expect, this process differs somewhat depending on what sort of control you are working with.

Our first example will be a modification of the temperature conversion program where we use text boxes rather than prompt( ) and alert( ) boxes.  You should copy this code into a file called FToCTextboxes.html.

 

<html>
<head>
<title>Fahrenheit to Celsius</title>

<script language = "JavaScript">
function FtoC()
{
var FDegrees = window.document.ConvertTemp.txtFahrenheit.value;

var CDegrees = (FDegrees - 32) * 5 / 9;

window.document.ConvertTemp.txtCelsius.value = CDegrees;
}
</script>

</head>

<body bgcolor="#666699" text="#C0C0C0" link="#660066" vlink="#660066">
<b><font size="6" color="#800000">Convert Temperatures in Fahrenheit to Celsius</font></b>
<p>&nbsp;</p>
<form name = "ConvertTemp">


<div align="left">
<table border="0" cellpadding="4" cellspacing="0" width="606">
<tr>
<td width="257">
<p align="right"><b><font size="4">Input for Degrees Fahrenheit</font></b></td>
<td width="329">
<input type="text" name="txtFahrenheit" size="20" style="font-size: 14pt; font-weight: bold"></td>
</tr>
<tr>
<td align="center" colspan="2" width="596">
<input type="button" value="Convert to Celsius" name="cmdConvert" style="font-size: 14pt; font-weight: bold" onClick="FtoC();"></td>
</tr>
</table>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<table border="0" cellpadding="4" cellspacing="0" width="606">
<tr>
<td width="256">
<p align="right"><b><font size="4">Output for Degrees Celsius</font></b></td>
<td width="330">
<input type="text" name="txtCelsius" size="20" style="font-size: 14pt; font-weight: bold"></td>
</tr>
</table>
<p>&nbsp;</p>

</form>

<p>&nbsp;</p>

</body>
</html>

 

You should actually see something like the following upon first entering the URL, though your inputs should differ and results are likely to differ.

 

 

Most of the code is associated with developing the form.  The real number crunching and information input/output is done very succinctly in FToC( ).  First we get the Fahrenheit temperature from the form and then this is used to calculate the result.  This result is then placed in the txtCelsius textbox with the following command.

window.document.ConvertTemp.txtCelsius.value = CDegrees;

This same approach works for both textboxes and textareas.

Resetting Defaults.  Now we are going to work with the three other basic control types in forms,

  1. checkboxes

  2. radio buttons

  3. select/drop down boxes

Each of these requires a somewhat different approach to be assigned values within the code.

First we will give the code which should be put in a file called ResetTheDefaults.html.

 

<html>
<head>
<title>Resetting the Defaults</title>

<script language = "JavaScript">
function ResetDefaultValues()
{
// resetting the defaults for the check boxes
window.document.ResetDefaults.chkProgramming.checked = true;
window.document.ResetDefaults.chkAnalysisDesign.checked = false;
window.document.ResetDefaults.chkSysAd.checked = true;

// resetting the default for the radio buttons
// setting default use level to intermittent

window.document.ResetDefaults.rbFreq[1].checked = true;

// resetting the select/drop down box
// setting default type of use to Games

window.document.ResetDefaults.cboType.options[2].selected = true;
}
</script>

</head>

<body bgcolor="#666699" text="#C0C0C0" link="#660066" vlink="#660066">
<b><font size="6" color="#800000">Reset the Defaults</font></b>
<p>&nbsp;</p>
<form name = "ResetDefaults">


<div align="center">
<center>
<table border="0" cellpadding="4" cellspacing="0" width="500">
<tr>
<td align="right"><b><font size="5" color="#000044">Interests:</font></b></td>
<td></td>
</tr>
<tr>
<td align="right"><font size="4"><b>Programming</b></font></td>
<td><input type="checkbox" name="chkProgramming" value="ON"></td>
</tr>
<tr>
<td align="right"><font size="4"><b>Analysis &amp; Design</b></font></td>
<td><input type="checkbox" name="chkAnalysisDesign" value="ON"></td>
</tr>
<tr>
<td align="right"><font size="4"><b>System Administration</b></font></td>
<td><input type="checkbox" name="chkSysAd" value="ON"></td>
</tr>
<tr>
<td align="right"><font size="4"><b>&nbsp; </b></font></td>
<td></td>
</tr>
<tr>
<td align="right"><b><font size="5" color="#000044">Frequency of Use:</font></b></td>
<td></td>
</tr>
<tr>
<td align="right"><font size="4"><b>Seldom</b></font></td>
<td><input type="radio" value="Seldom" name="rbFreq"></td>
</tr>
<tr>
<td align="right"><b><font size="4">Intermittent</font></b></td>
<td><input type="radio" value="Intermittent" name="rbFreq"></td>
</tr>
<tr>
<td align="right"><b><font size="4">Often</font></b></td>
<td><input type="radio" value="Often" name="rbFreq"></td>
</tr>
<tr>
<td align="right"><font size="4"><b>&nbsp; </b></font></td>
<td></td>
</tr>
<tr>
<td align="right"><b><font size="5" color="#000044">Type of Use:</font></b></td>
<td><select size="1" name="cboType" style="font-size: 14pt">
<option>Internet</option>
<option>Office</option>
<option>Games</option>
</select></td>
</tr>
<tr>
<td align="right"><font size="4"><b>&nbsp; </b></font></td>
<td></td>
</tr>
<tr>
<td align="right"><input type="button" value="Reset Defaults" name="cmdResetDefaults" style="font-size: 14pt" onClick="ResetDefaultValues();">
</td>
<td></td>
</tr>
</table>
</center>
</div>

</form>

<p>&nbsp;</p>

</body>
</html>

 

After you upload the page and access it, you should see something like the following after you select something and then click on the command button to reset the defaults.

 

 

Most of the code is actually associated with developing the form.  The code to reset the defaults is contained in ResetDefaultValues( ) To reset the checkboxes requires the use of the following code segment.

// resetting the defaults for the check boxes
window.document.ResetDefaults.chkProgramming.checked = true;
window.document.ResetDefaults.chkAnalysisDesign.checked = false;
window.document.ResetDefaults.chkSysAd.checked = true;

These statements just re-assign the checked property on each checkbox control.

To reset the radio buttons just requires one statement because all three are in a grouping.  The code to reset the radio buttons is

// resetting the default for the radio buttons
// setting default use level to intermittent

window.document.ResetDefaults.rbFreq[1].checked = true;

Remember the radio buttons are considered to be in the same array and setting one to be true requires the others to be false.  It is also important to remember the array numbering starts at 0.

Finally, the code for resetting the select/drop down box requires just one statement because only one option in the box can be selected.  The others are automatically unselected.  The code for resetting the select/drop down box is

// resetting the select/drop down box
// setting default type of use to Games

window.document.ResetDefaults.cboType.options[2].selected = true;

Again, an array is used to determine which option is selected.

Now you should be able to set the values of controls in a form whenever you want.