473,395 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Using Array in selectbox options

I have a bi-dimensional array that looks like this. I could split this
up any I need to if there is a better method.

{1,COMS}{2,IBM}{3,F}

I want to use this array to populate a selectbox. The numeric value is
the option value and the text is the text I want the user to see. I'm
not sure what to do with the array once I get the data back and
populate my selectbox.

// Split the response into an array
results = xmlHttp.responseText.split("{");
var optn = document.createElement("OPTION");
optn.text = results[0];
optn.value = results[1];
document.myForm.mySelectbox.options.add(optn);
This is the end result I'm looking for from the array above

<form id="myForm">
<select name="mySelectBox">
<option value="1">COMS</option>
<option value="2">IBM</option>
<option value="3">F</option>
</select>
</form>

Apr 12 '07 #1
4 3747
On Apr 12, 1:09 pm, "evanbu...@gmail.com" <kimvanbu...@gmail.com>
wrote:
I have a bi-dimensional array that looks like this. I could split this
up any I need to if there is a better method.

{1,COMS}{2,IBM}{3,F}
I'll assume that at this stage, your data is just as string, so try:

var s = '{1,COMS}{2,IBM}{3,F}';
var re = new RegExp('{([^{}]*)}','g');
var x = [];
while (re(s)){
x.push(RegExp.$1.split(','));
}
You may want to do some validation of the data on the way, otherwise
you might get some horrible results. Now use x to create the options:

var sel = document.forms['myForm'].elements['mySelectBox'];
for (var i=0, len=x.length; i<len; i++){
sel.options[i] = new Option(x[i][1], x[i][0]);
}

It needs a few feature tests, etc. to make it robust but that's your
job. :-)
--
Rob
Apr 12 '07 #2
On Apr 12, 3:47 pm, "RobG" <r...@iinet.net.auwrote:
[...]
I'll assume that at this stage, your data is just as string, so try:

var s = '{1,COMS}{2,IBM}{3,F}';
var re = new RegExp('{([^{}]*)}','g');
var x = [];
while (re(s)){
Sorry, didn't test in IE (doh), use:

while (re.exec(s)){
--
Rob
Apr 12 '07 #3
Elegant solution! though will struggle when parsing lots of data. I
did
a time comparison using this method compared to using spilts and the
2nd method was about 6 times faster...

in essence:

arr=s.split("}{");
loop over arr and perform arr[i].split(",") which will give you your
name/value pairs

don't forget to remove the leading { and trailing }

Pete

On Apr 12, 1:57 pm, "RobG" <r...@iinet.net.auwrote:
On Apr 12, 3:47 pm, "RobG" <r...@iinet.net.auwrote:
[...]
I'll assume that at this stage, your data is just as string, so try:
var s = '{1,COMS}{2,IBM}{3,F}';
var re = new RegExp('{([^{}]*)}','g');
var x = [];
while (re(s)){

Sorry, didn't test in IE (doh), use:

while (re.exec(s)){

--
Rob

Apr 12 '07 #4
On Apr 12, 4:40 am, "Pete" <peter.gal...@gmail.comwrote:
Elegant solution! though will struggle when parsing lots of data. I
did
a time comparison using this method compared to using spilts and the
2nd method was about 6 times faster...

in essence:

arr=s.split("}{");
loop over arr and perform arr[i].split(",") which will give you your
name/value pairs

don't forget to remove the leading { and trailing }

Pete

On Apr 12, 1:57 pm, "RobG" <r...@iinet.net.auwrote:
On Apr 12, 3:47 pm, "RobG" <r...@iinet.net.auwrote:
[...]
I'll assume that at this stage, your data is just as string, so try:
var s = '{1,COMS}{2,IBM}{3,F}';
var re = new RegExp('{([^{}]*)}','g');
var x = [];
while (re(s)){
Sorry, didn't test in IE (doh), use:
while (re.exec(s)){
--
Rob- Hide quoted text -

- Show quoted text -
Works like magic! Thank you both.

Apr 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tole | last post by:
hi all, i've got a selcetbox (multiple) which is filled by javascript. only problem is that i have aprox 1000 options to add to that select, and that adding lasts for 5-6 seconds. Even...
6
by: Piotr Pietrowski | last post by:
Hello everybody, I have a *big* problem. I thought its not that big problem for you professionals... Anyway, I have a begin date which has 3 dropdown boxes (day/Month/Year). The same for the...
7
by: Erwin Moller | last post by:
Hi, situation: I have a long list of options in multiple selectbox. The selectbox gets a scrollbar in that situation. Does anybody know if it is possible to scroll through the options with...
1
by: shankwheat | last post by:
I've been using this little script to change the <a href=""></a> value of a link which works well. <script language="javascript" type="text/javascript"> function ordering(sorder) { var link =...
1
by: TKapler | last post by:
I think i am quite experienced javascript programmer, but I got a problem. I have a selectbox with e.g. 17 optgroups with 100 options. I need a javascript code to hide some of that optgroups (i...
4
by: shankwheat | last post by:
I'm passing a string like 'Div3,Div4' to use as options in a selectbox but I need the text the user sees in the box different from the values. My code populates the values in the selectbox...
2
by: shankwheat | last post by:
I have a form named 'choiceForm' and 2 select boxes named 'available' and 'move'. I give users the ability of copying options (not moving) from available to move with the code below. I would like...
4
by: patelxxx | last post by:
I'm trying to use the DIV tag instead of the FORM tag which I'm currently using in my code to create a 'drop down list'. But I'm getting the ERROR: 'document.drop_list.locationID' is null or not an...
4
by: ramchml | last post by:
i have two select boxes.based on the selected value in one select box it show the maximum options in the other select box in next page.i used javascript onclick function.for first time it shows...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.