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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Putting a select list into an array

Status
Not open for further replies.

Albuckj

Programmer
Jan 10, 2002
11
GB
This piece of JS copies the contents of a select list into an array, and it works fine:

<script>
ops = new Array();
var total = document.form1.poss.options.length;
document.write(&quot;Select box option numbers: &quot;+total+&quot;<BR>&quot;);
for (x=0;x<total;x++)
{
ops[x] = document.form1.poss.options[x].text;
document.write(ops[x]+&quot;<BR>&quot;);
}
</script>

However, to do exactly the same thing with another select box (name=chosen), it doesn't work:

function checker()
{
new1 = new Array();
var newlen = document.form1.chosen.options.length;
document.write(&quot;Chosen box option numbers: &quot;+newlen+&quot;<BR>&quot;);
for (k=0;k<newlen;k++)
{
new1[k] = document.form1.chosen.options[k].text;
document.write(new1[k]+&quot;<BR>&quot;);
}
}

At the line: new1[k] = document.form1.chosen.options[k].text;
it gives the following error:

'document.form1.chosen' is not an object

What's strange is that it writes the correct length of this select box (e.g. - 4), so must obviously recognise it as an object if it can get the length of the select box via the 'options' word. Has anyone got any idea why this happens?????

Here is the full code in case you need it:
<html>
<head>
<title>Multiple Select Boxes</title>
<script>
function copyToList(from,to)
{
fromList = eval('document.forms[0].' + from);
toList = eval('document.forms[0].' + to);
if (toList.options.length > 0 && toList.options[0].value == 'temp')
{
toList.options.length = 0;
}
var sel = false;
for (y=0;y<fromList.options.length;y++)
{
var current = fromList.options[y];
if (current.selected)
{
sel = true;
if (current.value == 'temp')
{
alert ('You cannot move this text!');
return;
}
txt = current.text;
val = current.value;
toList.options[toList.length] = new Option(txt,val);
fromList.options[y] = null;
y--;
}
}
checker();
if (!sel) alert ('You haven\'t selected any options!');
}

function allSelect()
{
List = document.forms[0].chosen;
if (List.length && List.options[0].value == 'temp') return;
for (y=0;i<List.length;y++)
{
List.options[y].selected = true;
}
}

function checker()
{
new1 = new Array();
var newlen = document.form1.chosen.options.length;
document.write(&quot;Chosen box option numbers: &quot;+newlen+&quot;<BR>&quot;);
for (k=0;k<newlen;k++)
{
new1[k] = document.form1.chosen.options[k].text;
document.write(new1[k]+&quot;<BR>&quot;);
}
}

</script>
</head>

<body>
<Table><TR><TD>
<FORM name=&quot;form1&quot; onSubmit=&quot;allSelect()&quot; ACTION=&quot;/cgi-bin/broker&quot; METHOD=&quot;post&quot;>
<SELECT NAME=&quot;poss&quot; SIZE=&quot;20&quot; MULTIPLE WIDTH=200 STYLE=&quot;width: 200px&quot;>
<option value=1>AWD Brokers
<option value=2>Barclays
<option value=3>Casenove
<option value=4>Frankie Plc.
<option value=5>HSBC
<option value=6>Marsh Financial Services
<option value=7>PNO Euro
<option value=8>Spain brokers
<option value=9>UK
<option value=10>Wellington Finance
</SELECT></TD>
<TD>
<A HREF=&quot;javascript:copyToList('poss','chosen')&quot;>--&gt;</A><BR>
<A HREF=&quot;javascript:copyToList('chosen','poss')&quot;>&lt;--</A>
</TD>
<TD>
<SELECT NAME=&quot;chosen&quot; SIZE=&quot;20&quot; MULTIPLE WIDTH=200 STYLE=&quot;width: 200px&quot;>
<OPTION VALUE=&quot;temp&quot;>
</SELECT>
</TD>
</TR>
<TR><TD><BR><INPUT TYPE=&quot;SUBMIT&quot;></FORM></TD></TR>
</TABLE>
<script>
ops = new Array();
var total = document.form1.poss.options.length;
document.write(&quot;Select box option numbers: &quot;+total+&quot;<BR>&quot;);
for (x=0;x<total;x++)
{
ops[x] = document.form1.poss.options[x].text;
document.write(ops[x]+&quot;<BR>&quot;);
}
</script>
</body>
</html>


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top