Javascript and <select multiple>

lomn75

Purple Ace
Joined
Jun 26, 2000
Messages
6,613
OK, I've got a form that I dynamically change the contents of via javascript with commands like
Code:
document.forms.formname.selectfieldname.options.length = 0
That's all good for regular select fields.

The problem is, I want to do this for a field of the type
Code:
<select multiple name="selectfieldname[]">
(allows user to hold CTRL and select multiple options)

The problem is that this now breaks the Javascript used to rewrite the form. Like this, I get
'document.forms.formname.selectfieldname.options' is null or not an object
If I change the javascript to
Code:
document.forms.formname.selectfieldname[].options.length = 0
I get
syntax error
and if I try
Code:
document.forms.formname.selectfieldname[0].options.length = 0
I get
'document.forms.formname.selectfieldname.0' is null or not an object

Anybody know the proper way to approach this?

Alternately, is there a way to get all the values in PHP without adding that "[]" to the select name? doing $_POST["selectfieldname"] without the "[]" in the form gives me only one selected item, and it's not an array that I can parse with a foreach() or whatever.
 
Never mind, found a fix. For the record, instead of
Code:
document.form.fieldname.options
you refer to
Code:
document.form.elements[x].options
and it quits complaining about the element name being "fieldname[]"
 
Back
Top