...

View Full Version : populate combo from array



vikaspa
07-05-2004, 11:41 AM
currentTD = currentTR.insertCell();
currentTD.innerHTML = "<div align='center'> <select name='conatinertype"+inputCount+"'> <option value='1'>OT</option> <option value='2'>TR</option> <option value='3'>GP</option> <option value='4'>RF</option> </select>";

a row is inserted using javascript and above code generate combo .

My

The values are static
How do I populate a combo from an array .

I donot know the length of the same(or no. of entries not known_.

jbot
07-05-2004, 11:48 AM
currentTD = currentTR.insertCell();
currentTD.innerHTML = "<div align='center'> <select name='conatinertype"+inputCount+"'> <option value='1'>OT</option> <option value='2'>TR</option> <option value='3'>GP</option> <option value='4'>RF</option> </select>";

a row is inserted using javascript and above code generate combo .

My

The values are static
How do I populate a combo from an array .

I donot know the length of the same(or no. of entries not known_.

never use innerHTML to generate combos, since they don't generate them properly.

use the document.createElement and document.appendChild methods to create the selectbox, then loop thru the array and use the new Option constructor to add options to the dropdown.

glenngv
07-05-2004, 11:55 AM
There are many ways to populate a combo, but do you mean it like this?


currentTD.innerHTML = '<div align="center"><select name="conatinertype'+inputCount+'">' + generateOptions() + '</select></div>';
...
function generateOptions(){
var opt='';
for (var i=0;i<globalArray.length;i++){
opt+='<option value="'+globalArray[i][0]+'">'+globalArray[i][1]+'</option>\n';
}
return opt;
}

This assumes that your array is a "2D" array, the first column refers to the option value, the 2nd column the option text.
If the array is not global, you can pass it as the function parameter.

vikaspa
07-13-2004, 04:20 PM
Dear glenngv


Thanks a lot for your valued help

Vincent Puglia
07-14-2004, 02:19 AM
Hi,

Here's another method:


<form name='theForm'>
<select name="theSel">
</select>
</form>
<script type="text/javascript" language="javascript">
var theArray = new Array('OT',1, 'TR',2, 'GP', 3, 'RF',4);

var selObj = document.theForm.theSel
for (var i = 0; i < theArray.length; i++)
selObj.options[selObj.length] = new Option(theArray[i++], theArray[i])
</script>


Vinny

vikaspa
07-14-2004, 04:21 AM
Dear Vinny

I appreciate this

your methos is also good one !

Vikas

Vincent Puglia
07-14-2004, 01:26 PM
Hi Vikaspa,

You're welcome :)

Vinny



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum