Using Array in selectbox options

E

evanburen

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>
 
R

RobG

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 = new Option(x[1], x[0]);
}

It needs a few feature tests, etc. to make it robust but that's your
job. :)
 
R

RobG

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)){
 
P

Pete

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.split(",") which will give you your
name/value pairs

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

Pete

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)){
 
E

evanburen

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.split(",") which will give you your
name/value pairs

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

Pete

[...]
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)){

- Show quoted text -


Works like magic! Thank you both.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top