Martha's COIN 70b JavaScript Test Form

Select Options

Midterm Practice


TEST

FORM ELEMENTS:

<form>
<select name="example" onchange="react(this);">
<option value="Number One" selected>1</option>
<option value="The Second">2</option>
<option value="Three is It">3</option>
</select>
</form>
CODE TESTED:

function react(select){
 alert("example.options[1].value = " + select.options[1].value);
 alert("example.options[2].text = " + select.options[2].text);
 alert("example.selectedIndex = " + select.selectedIndex);
 alert("example.options[1].defaultSelected = " + select.options[1].defaultSelected);
 alert("example.options[1].selected = " + select.options[1].selected);
 alert("example.length = " + select.length);
 alert("example.type = " + select.type);
 alert("example.name = " + select.name);

CODE TESTED

document.write("This is a test.");
document.write("Options 1 value is: " + example.options[1].value);
alert("Test Failed");:

PROBLEM - example.options[1].value is "not defined". How can that be?

SOLUTION - should be written as: document.forms[0].example.options[1].value); need to write complete path!!