Javascript Options Checking

Richard Korts
Richard Korts used Ask the Experts™
on
See attached html.

On line 100 there is a select element defined. I gave it the name fields[] so that when it is passed to the php processing program, it is recognized by php as an array.

Note on line 88 the onsubmit = "return chk_vals();". The purpose is to make sure the user selected at least ONE value from the select list named fields[].

Note on lines 39 - 52 the function chk_vals().

When I run the program (in Firefox), it says syntax error on line 42. I'm guessing it doesn't like the fields[].

How do I achieve what I want?
custs-by-criteria-sel.htm
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
first option, add an ID attribute :
<select name="fields[]" size="20" multiple class="pt12" id="fields">

and replace :
document.st.fields[]
by :
document.getElementById("fields")
leakim971Multitechnician
Top Expert 2014

Commented:
second option :
replace :
document.st.fields[].options[i].selected

Open in new window

by :
document.getElementsByName("fields[]")[0].options[i].selected

Open in new window

Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
You would be right because you did not use an index like fields[0] to define which array element you were looking for.  I don't believe you can leave that blank and have it work.  It is basically undefined as you have it.
leakim971Multitechnician
Top Expert 2014

Commented:
another option (the worst one):
replace :
document.st.fields[].options[i].selected

Open in new window

by :
document.st["fields[]"].options[i].selected

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start Today