| [main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql] | |
Note that all external links will open up in a separate window. This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser. |
Form ElementsForm Elements
All of the properties you set for form fields with HTML are programatically accessible with JavaScript, however there are just a few key elements that we need concern ourselves with. The first and best hidden is the
The two most important properties that you declare as attributes of the form elements are
Most form elements also have two methods you can make use of. These are <form name="form01" method="" action=""> <p> Watch the cursor ... <input type="text" name="field01" size="10" /> <input type="button" name="field02" value="Focus" onclick="document.form01.field01.focus();" /> <input type="button" name="field03" value="Blur" onclick="document.form01.field01.blur();" /> </p> </form> Radio Buttons
Many form elements need to have unique names. The one exception to this rule is the radio button. Values from radio buttons are stored in an array of values. The name of the array is the same as the name of the field itself, but the values for the field are stored as an array of values, one for each radio button defined for that element. The way in which you determine which option is selected is by walking the array and seeing which element has a
<form class="example" name="form02" method="" action="">
<p>
Pick an option ...
[<input type="radio" name="field01" value="item1" /> 1]
[<input type="radio" name="field01" value="item2" /> 2]
[<input type="radio" name="field01" value="item3" /> 3]
[<input type="radio" name="field01" value="item4" /> 4]
</p>
<p>
And click the button:
<input type="button" name="field02" value="Find the One"
onclick="findtheone( );" />
<input type="button" name="field03" value="Set to One"
onclick="document.form02.field01[0].checked='true';" />
</p>
</form>
<script type="text/javascript" language="JavaScript">
<!--
function findtheone( ) {
var whichitem = 0;
while (whichitem < document.form02.field01.length) {
if (document.form02.field01[whichitem].checked) {
window.alert('You picked item number ' + (whichitem + 1));
}
whichitem++;
}
}
// -->
</script>
You can do the same thing with Checkboxes. If you have a collection of checkboxes with the same name you can address them as an array, testing for which ones are selected by checking the
<form class="example" name="form03" method="" action="">
<p>
Pick an option ...
<br />
Row 1: [<input type="checkbox" name="field01" value="item1" /> 1]
<br />
Row 2:
[<input type="checkbox" name="field02" value="item1" /> 1]
[<input type="checkbox" name="field02" value="item2" /> 2]
[<input type="checkbox" name="field02" value="item3" /> 3]
</p>
<p>
And click the button:
<input type="button" name="field03" value="Find the One"
onclick="findtheone( );" />
<input type="button" name="field04" value="Set to One"
onclick="document.form03.field01.checked='true';" />
</p>
</form>
<script type="text/javascript" language="JavaScript">
<!--
function findtheone( ) {
var outvar = '';
var whichitem = 0;
if (document.form03.field01.checked) {
outvar = 'You selected the item in the first row. \n';
}
while (whichitem < document.form03.field02.length) {
if (document.form03.field02[whichitem].checked) {
outvar = outvar + 'You selected item number ' +
(whichitem + 1) + ' in the second row. \n';
}
whichitem++;
}
window.alert(outvar);
}
// -->
</script>
Selectable Elements
Drop down menus and scrollable menus are made of selectable elements. Each selection set is made up of a containing
The
The easiest way to determine which option has been selected in a selectable list is by using the
If you have Options have some properties worthy of note.
<form class="example" name="form04" method="" action="">
<p>
Pick an option ...
<select name="field01" size="6">
<option value="a">Door number 1</option>
<option value="b">Door number 2</option>
<option value="c">Door number 3</option>
<option value="d">This box</option>
</select>
Pick some options ...
<select multiple="multiple" name="field02" size="6">
<option value="a">Door number 1</option>
<option value="b">Door number 2</option>
<option value="c">Door number 3</option>
<option value="d">This box</option>
</select>
</p>
<p>
And click the button:
<br />
<input type="button" name="field03" value="Set to One"
onclick="document.form04.field01.options[0].selected='true';" />
<input type="button" name="field04" value="Set to New"
onclick="document.form04.field01.options[3].text='A new car';" />
<input type="button" name="field05" value="Set to Old"
onclick="document.form04.field01.options[3].text='This box';" />
<input type="button" name="field06" value="Find the One"
onclick="findtheone( );" />
<input type="button" name="field07" value="Find the Many"
onclick="findthemany( );" />
<input type="button" name="field08" value="Door number 4"
onclick="document.form04.field01.options[document.form04.field01.length]=
new Option('Door Number 4', 'e');" />
</p>
</form>
<script type="text/javascript" language="JavaScript">
<!--
function findtheone( ) {
if (document.form04.field01.options[document.form04.field01.selectedIndex]) {
window.alert('You selected item ' +
(document.form04.field01.selectedIndex + 1));
}
else {
window.alert('You did not select and item');
}
}
function findthemany( ) {
var outvar = '';
var whichitem = 0;
while (whichitem < document.form04.field02.length) {
if (document.form04.field02.options[whichitem].selected) {
outvar = outvar + ('You selected item ' + (whichitem + 1) + ': ' +
document.form04.field02.options[whichitem].text) + '\n';
}
whichitem++;
}
if (outvar == '') { outvar = 'You did not select an item.'; }
window.alert(outvar);
}
// -->
</script>
These pages can be found at:
[http://academ.hvcc.edu/~kantopet/]
|