You are here: irt.org | FAQ | JavaScript | Form | 3.1 | Q589 [ previous next ]
The following example will attempt to "subtract" one part of the form reference from another:
<script language="JavaScript"><!-- function setAction() { alert(document.formName.select-name.options[document.formName.select-name.selectedIndex].value); return false; } //--></script> <form name="formName" onSubmit="return setAction()"> <select name="select-name"> <option selected value="yes.html">Yes <option value="no.html">No </select> <input type="submit"> </form>
To avoid this refer to the form element using the forms elements array:
<script language="JavaScript"><!-- function setAction() { alert(document.formName.elements['select-name'].options[document.formName.elements['select-name'].selectedIndex].value); return false; } //--></script> <form name="formName" onSubmit="return setAction()"> <select name="select-name"> <option selected value="yes.html">Yes <option value="no.html">No </select> <input type="submit"> </form>