Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q589 How can I refer to a dropdown select box that has a non alphanumeric name?

irt.org | Knowledge Base | JavaScript | Form 3.1 | Q589 [ previous next ]

Q589 How can I refer to a dropdown select box that has a non alphanumeric name?

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>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.