Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changing multiple select

Status
Not open for further replies.

conrai

Programmer
Nov 21, 2004
10
FI
Hi!

I'm struggling with the following problem and would be grateful if you could help:

I have a multiple select field:
Code:
<select multiple size="5" name="fields[]">
  <option value ="value">option1</option>
  ...
</select>
Please note that the name is "fields[]", thus an array, because I want to be able to access all selected options values $_POSTed on the next page. So far this works.

The problem is that I have to fill the select menu dynamically (the values for the options come from a database) with JScript.

If my select menu was not multiple, thus if i name it only "fields" instead of "fields[]", I know hot to e.g. add new options:
Code:
addOption(document.all.fields, text, value);
Code:
function addOption(selectObject, optionText, optionValue) {
  var optionObject = new Option(optionText,optionValue);
  var optionRank = selectObject.options.length;
  selectObject.options[optionRank]=optionObject;
}
How do I have to modify the addOption function, so that i can insert options to the "fields[]" array?

And how will the function be called then,
addOption(document.all.fields[], text, value); ??

Thanks already now for your help!

c.
 

If you want your code to work in anything other than IE, you'll need drop the "document.all" thing anyway. One way would be to give the form element an ID as well as a name, and use that:

Code:
<select multiple size="5" name="fields[]" id="myId">

...

addOption(document.getElementById('myId'), text, value);

Hope this helps,
Dan



[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Thanks Dan, this works! I will use document.getElementById() in the future, promised ;-).

conrad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top