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!

Getting the values of an array from a javascript file into a combobox. 2

Status
Not open for further replies.

SuperSal

Programmer
Mar 13, 2007
33
Hiya everyone, just wondered if anyone knows how to get the values from an array from an external javascript file into a combo/list box. The whole idea is that the javascript file will contain all the fields needed for all the comboboxes and this file will be used by a large number of interfaces.

say I had a settings.js file with this array:

var JPEGHighRes = new Array("1600x1200", "800x600", "640x480")

could i get these values into a combobox like below?

<select name="comboJHigh" id="comboJHigh">
<option value="0">JPEGHighRes[0]</script></option>
<option value="1">JPEGHighRes[2]</option>
<option value="2">JPEGHighRes[2]</option></select>

I have tried various ways but not successfully so if anyone has any ideas then let me know

Thanks
Sally
 
You will need to use DHTML.

Put this code AFTER the <select> is created or on a window.onload.
Code:
<script type="text/javascript" src="settings.js"></script>
<select name="comboJHigh" id="comboJHigh">
<option></option>
</select>
<script type="text/javascript">
  var selectBox = document.getElementById("comboJHigh");
  var resArrayLength = JPEGHighRes.length;
  selectBox.options.length = 0;
  for (a = 0; a < resArrayLength; a++) {
    selectBox.options[a] = new Option(JPEGHighRes, a);
  }
</script>

*I put an empty <option> to make it valid XHTML

[monkey][snake] <.
 
monksnake(or anyone else), thanks for your post 11 Jul 07 11:06. Sorry I have only just got round to testing this :) The code works, however it puts all the array values in for each selection. I have tried playing around with the code but have been unsuccessful.

The code is as follows:

<BODY onLoad="addOption()";>

var Quality = new Array("High 2", "High 3", "High 4", "High 5", "High 6", "High 7", "High 8", "High 9", "High 10", "High 11", "Med 12", "Med 13", "Med 14", "Med 15", "Med 16", "Med 17", "Med 18", "Med 19", "Med 20", "Med 21" , "Low 22" , "Low 23" , "Low 24" , "Low 25" , "Low 26" , "Low 27" , "Low 28" , "Low 29" , "Low 30" , "Low 31")

function addOption(selectBox,resArrayLength)
{
var selectBox = document.getElementById("MHighQty");
selectBox.options.length = 0;
for (a = 0; a < Quality.length; a++) {
selectBox.options[a] = new Option(Quality, a);
}
}

<select name="MHighQty" id="MHighQty"><option></option></select>';

Is there anything that you can see which makes all the values from the array appear for each selection in the combobox.

Thanks

Sally
 
Yeah, this:
Code:
selectBox.options[a] = new Option(Quality, a);

becomes this:
Code:
selectBox.options[a] = new Option(Quality[a], a);

which means that I had a bug in my example above [cry]

[monkey][snake] <.
 
Thanks Dan and Monksnake, much appreciated.

Sal
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top