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("Select box option numbers: "+total+"<BR>"
for (x=0;x<total;x++)
{
ops[x] = document.form1.poss.options[x].text;
document.write(ops[x]+"<BR>"
}
</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("Chosen box option numbers: "+newlen+"<BR>"
for (k=0;k<newlen;k++)
{
new1[k] = document.form1.chosen.options[k].text;
document.write(new1[k]+"<BR>"
}
}
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("Chosen box option numbers: "+newlen+"<BR>"
for (k=0;k<newlen;k++)
{
new1[k] = document.form1.chosen.options[k].text;
document.write(new1[k]+"<BR>"
}
}
</script>
</head>
<body>
<Table><TR><TD>
<FORM name="form1" onSubmit="allSelect()" ACTION="/cgi-bin/broker" METHOD="post">
<SELECT NAME="poss" SIZE="20" MULTIPLE WIDTH=200 STYLE="width: 200px">
<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="javascript:copyToList('poss','chosen')">--></A><BR>
<A HREF="javascript:copyToList('chosen','poss')"><--</A>
</TD>
<TD>
<SELECT NAME="chosen" SIZE="20" MULTIPLE WIDTH=200 STYLE="width: 200px">
<OPTION VALUE="temp">
</SELECT>
</TD>
</TR>
<TR><TD><BR><INPUT TYPE="SUBMIT"></FORM></TD></TR>
</TABLE>
<script>
ops = new Array();
var total = document.form1.poss.options.length;
document.write("Select box option numbers: "+total+"<BR>"
for (x=0;x<total;x++)
{
ops[x] = document.form1.poss.options[x].text;
document.write(ops[x]+"<BR>"
}
</script>
</body>
</html>
<script>
ops = new Array();
var total = document.form1.poss.options.length;
document.write("Select box option numbers: "+total+"<BR>"
for (x=0;x<total;x++)
{
ops[x] = document.form1.poss.options[x].text;
document.write(ops[x]+"<BR>"
}
</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("Chosen box option numbers: "+newlen+"<BR>"
for (k=0;k<newlen;k++)
{
new1[k] = document.form1.chosen.options[k].text;
document.write(new1[k]+"<BR>"
}
}
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("Chosen box option numbers: "+newlen+"<BR>"
for (k=0;k<newlen;k++)
{
new1[k] = document.form1.chosen.options[k].text;
document.write(new1[k]+"<BR>"
}
}
</script>
</head>
<body>
<Table><TR><TD>
<FORM name="form1" onSubmit="allSelect()" ACTION="/cgi-bin/broker" METHOD="post">
<SELECT NAME="poss" SIZE="20" MULTIPLE WIDTH=200 STYLE="width: 200px">
<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="javascript:copyToList('poss','chosen')">--></A><BR>
<A HREF="javascript:copyToList('chosen','poss')"><--</A>
</TD>
<TD>
<SELECT NAME="chosen" SIZE="20" MULTIPLE WIDTH=200 STYLE="width: 200px">
<OPTION VALUE="temp">
</SELECT>
</TD>
</TR>
<TR><TD><BR><INPUT TYPE="SUBMIT"></FORM></TD></TR>
</TABLE>
<script>
ops = new Array();
var total = document.form1.poss.options.length;
document.write("Select box option numbers: "+total+"<BR>"
for (x=0;x<total;x++)
{
ops[x] = document.form1.poss.options[x].text;
document.write(ops[x]+"<BR>"
}
</script>
</body>
</html>