468,538 Members | 1,504 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,538 developers. It's quick & easy.

multiple select

Hello,
I would appreciate any assistance you have with the following
problem:

I have (as an example) an array of values as follows:
arrayvalues=new Array("0001","0003","0005") where each is the value
of an option in a select statement:

<select id="usertypes" multiple="multiple">
<option value="0033">data1</option>
<option value="0025">data2</option>
<option value="0001">data3</option>
<option value="0003">data4</option>
<option value="1234">data5</option>
<option value="0005">data6</option>
</select>

Based on my array values I would like to highlight each option whose
value cooresponds to an element of my array. I have the following
javascript code:

usertypes=document.getElementById("usertypes");
for(var j=0; j<arrayvalues.length; j++)
for(var i=0; i<usertypes.options.length; i++)
{
if(usertypes.options[i].value==arrayvalues[j])
usertypes.options[i].selected=true;
}

Problem: Script will not highlight all options associated with my
array values. However, if I add an alert statement before or after
the if statement, the script will highlight each entry as needed.
Does anyone have any ideas on why this is happening?
Jul 31 '06 #1
4 2210

Matt Ratliff wrote:
Hello,
I would appreciate any assistance you have with the following
problem:

I have (as an example) an array of values as follows:
arrayvalues=new Array("0001","0003","0005") where each is the value
of an option in a select statement:

<select id="usertypes" multiple="multiple">
<option value="0033">data1</option>
<option value="0025">data2</option>
<option value="0001">data3</option>
<option value="0003">data4</option>
<option value="1234">data5</option>
<option value="0005">data6</option>
</select>

Based on my array values I would like to highlight each option whose
value cooresponds to an element of my array. I have the following
javascript code:

usertypes=document.getElementById("usertypes");
for(var j=0; j<arrayvalues.length; j++)
for(var i=0; i<usertypes.options.length; i++)
{
if(usertypes.options[i].value==arrayvalues[j])
usertypes.options[i].selected=true;
}

Problem: Script will not highlight all options associated with my
array values. However, if I add an alert statement before or after
the if statement, the script will highlight each entry as needed.
Does anyone have any ideas on why this is happening?
I used the follwing to try to repeat the problem (based on assumptions
of your code fragments):

<script>
var arrayvalues=new Array("0001","0003","0005");

function setSelects(){
usertypes=document.getElementById("usertypes");
for(var j=0; j<arrayvalues.length; j++)
for(var i=0; i<usertypes.options.length; i++)
{
if(usertypes.options[i].value==arrayvalues[j])
usertypes.options[i].selected=true;
}
}
window.onload=setSelects;
</script>

<form>
<select id="usertypes" multiple="multiple">
<option value="0033">data1</option>
<option value="0025">data2</option>
<option value="0001">data3</option>
<option value="0003">data4</option>
<option value="1234">data5</option>
<option value="0005">data6</option>
</select>
</form>

However, it appears to work as you want it to (Firefox
1.5.05/Linux/Win2k & IE6).
Maybe the problem is how you are starting the script ?

Being one of those types who prefers to avoid nested loops, let me
suggest a little change to the script:
function setSelects(){
var inverted = new Array();
for(var i=0; i<arrayvalues.length; i++){
inverted[arrayvalues[i]] = true;
}

usertypes=document.getElementById("usertypes");
for(var i=0; i<usertypes.options.length; i++)
{
var curOpt = usertypes.options[i];
curOpt.selected = inverted[curOpt.value];
}
}

Jul 31 '06 #2
On 31 Jul 2006 08:04:06 -0700, "Paul" <pa********@gmail.comwrote:
>
Matt Ratliff wrote:
>Hello,
I would appreciate any assistance you have with the following
problem:

I have (as an example) an array of values as follows:
arrayvalues=new Array("0001","0003","0005") where each is the value
of an option in a select statement:

<select id="usertypes" multiple="multiple">
<option value="0033">data1</option>
<option value="0025">data2</option>
<option value="0001">data3</option>
<option value="0003">data4</option>
<option value="1234">data5</option>
<option value="0005">data6</option>
</select>

Based on my array values I would like to highlight each option whose
value cooresponds to an element of my array. I have the following
javascript code:

usertypes=document.getElementById("usertypes");
for(var j=0; j<arrayvalues.length; j++)
for(var i=0; i<usertypes.options.length; i++)
{
if(usertypes.options[i].value==arrayvalues[j])
usertypes.options[i].selected=true;
}

Problem: Script will not highlight all options associated with my
array values. However, if I add an alert statement before or after
the if statement, the script will highlight each entry as needed.
Does anyone have any ideas on why this is happening?
I used the follwing to try to repeat the problem (based on assumptions
of your code fragments):

<script>
var arrayvalues=new Array("0001","0003","0005");

function setSelects(){
usertypes=document.getElementById("usertypes");
for(var j=0; j<arrayvalues.length; j++)
for(var i=0; i<usertypes.options.length; i++)
{
if(usertypes.options[i].value==arrayvalues[j])
usertypes.options[i].selected=true;
}
}
window.onload=setSelects;
</script>

<form>
<select id="usertypes" multiple="multiple">
<option value="0033">data1</option>
<option value="0025">data2</option>
<option value="0001">data3</option>
<option value="0003">data4</option>
<option value="1234">data5</option>
<option value="0005">data6</option>
</select>
</form>

However, it appears to work as you want it to (Firefox
1.5.05/Linux/Win2k & IE6).
Maybe the problem is how you are starting the script ?

Being one of those types who prefers to avoid nested loops, let me
suggest a little change to the script:
function setSelects(){
var inverted = new Array();
for(var i=0; i<arrayvalues.length; i++){
inverted[arrayvalues[i]] = true;
}

usertypes=document.getElementById("usertypes");
for(var i=0; i<usertypes.options.length; i++)
{
var curOpt = usertypes.options[i];
curOpt.selected = inverted[curOpt.value];
}
}
Paul:
Thanks for the response. I agree about the nested loops. My
actual code doesnt use nested loops but I wanted to try and describe
the problem in a way that could be more easily understood. Here is my
actual code snippet:

say '<script language="javascript">'
say 'opts=parent.document.getElementById("misc3");'
do zz=1 to xref4s~items
say 'opts.options['xref4s[zz]['FARMTYPEUID']-1'].selected=true;'
end
say '</script>'

xref4s is a server side array where xref4s[1]['FARMTYPEUID']
contains "0001" for example. My form data is passed to a hidden
iframe that contains the above code. The resulting code from the
script will look like:

opts.options[1].selected=true
opts.options[3].selected=true
opts.options[5].selected=true

when my xref4s array contains ("0002", "0004", "0006")

The strange thing is the fact that if I add an alert inside the do
loop then it properly selects all related options.

Jul 31 '06 #3

Matt Ratliff wrote:
On 31 Jul 2006 08:04:06 -0700, "Paul" <pa********@gmail.comwrote:

Matt Ratliff wrote:
Hello,
I would appreciate any assistance you have with the following
problem:

I have (as an example) an array of values as follows:
arrayvalues=new Array("0001","0003","0005") where each is the value
of an option in a select statement:

<select id="usertypes" multiple="multiple">
<option value="0033">data1</option>
<option value="0025">data2</option>
<option value="0001">data3</option>
<option value="0003">data4</option>
<option value="1234">data5</option>
<option value="0005">data6</option>
</select>

Based on my array values I would like to highlight each option whose
value cooresponds to an element of my array. I have the following
javascript code:

usertypes=document.getElementById("usertypes");
for(var j=0; j<arrayvalues.length; j++)
for(var i=0; i<usertypes.options.length; i++)
{
if(usertypes.options[i].value==arrayvalues[j])
usertypes.options[i].selected=true;
}

Problem: Script will not highlight all options associated with my
array values. However, if I add an alert statement before or after
the if statement, the script will highlight each entry as needed.
Does anyone have any ideas on why this is happening?
I used the follwing to try to repeat the problem (based on assumptions
of your code fragments):

<script>
var arrayvalues=new Array("0001","0003","0005");

function setSelects(){
usertypes=document.getElementById("usertypes");
for(var j=0; j<arrayvalues.length; j++)
for(var i=0; i<usertypes.options.length; i++)
{
if(usertypes.options[i].value==arrayvalues[j])
usertypes.options[i].selected=true;
}
}
window.onload=setSelects;
</script>

<form>
<select id="usertypes" multiple="multiple">
<option value="0033">data1</option>
<option value="0025">data2</option>
<option value="0001">data3</option>
<option value="0003">data4</option>
<option value="1234">data5</option>
<option value="0005">data6</option>
</select>
</form>

However, it appears to work as you want it to (Firefox
1.5.05/Linux/Win2k & IE6).
Maybe the problem is how you are starting the script ?

Being one of those types who prefers to avoid nested loops, let me
suggest a little change to the script:
function setSelects(){
var inverted = new Array();
for(var i=0; i<arrayvalues.length; i++){
inverted[arrayvalues[i]] = true;
}

usertypes=document.getElementById("usertypes");
for(var i=0; i<usertypes.options.length; i++)
{
var curOpt = usertypes.options[i];
curOpt.selected = inverted[curOpt.value];
}
}

Paul:
Thanks for the response. I agree about the nested loops. My
actual code doesnt use nested loops but I wanted to try and describe
the problem in a way that could be more easily understood. Here is my
actual code snippet:

say '<script language="javascript">'
say 'opts=parent.document.getElementById("misc3");'
do zz=1 to xref4s~items
say 'opts.options['xref4s[zz]['FARMTYPEUID']-1'].selected=true;'
end
say '</script>'

xref4s is a server side array where xref4s[1]['FARMTYPEUID']
contains "0001" for example. My form data is passed to a hidden
iframe that contains the above code. The resulting code from the
script will look like:

opts.options[1].selected=true
opts.options[3].selected=true
opts.options[5].selected=true

when my xref4s array contains ("0002", "0004", "0006")

The strange thing is the fact that if I add an alert inside the do
loop then it properly selects all related options.
Ahh, now that is more informative. I suspect that the javascript is
executing before the browser has built the references to the html
components. Using the javascript console on Mozilla/Firefox should
confirm this.
Try wrapping that up in a function called after the page loads.

Jul 31 '06 #4
On 31 Jul 2006 10:14:55 -0700, "Paul" <pa********@gmail.comwrote:
>
Matt Ratliff wrote:
>On 31 Jul 2006 08:04:06 -0700, "Paul" <pa********@gmail.comwrote:
>
Matt Ratliff wrote:
Hello,
I would appreciate any assistance you have with the following
problem:

I have (as an example) an array of values as follows:
arrayvalues=new Array("0001","0003","0005") where each is the value
of an option in a select statement:

<select id="usertypes" multiple="multiple">
<option value="0033">data1</option>
<option value="0025">data2</option>
<option value="0001">data3</option>
<option value="0003">data4</option>
<option value="1234">data5</option>
<option value="0005">data6</option>
</select>

Based on my array values I would like to highlight each option whose
value cooresponds to an element of my array. I have the following
javascript code:

usertypes=document.getElementById("usertypes");
for(var j=0; j<arrayvalues.length; j++)
for(var i=0; i<usertypes.options.length; i++)
{
if(usertypes.options[i].value==arrayvalues[j])
usertypes.options[i].selected=true;
}

Problem: Script will not highlight all options associated with my
array values. However, if I add an alert statement before or after
the if statement, the script will highlight each entry as needed.
Does anyone have any ideas on why this is happening?
I used the follwing to try to repeat the problem (based on assumptions
of your code fragments):

<script>
var arrayvalues=new Array("0001","0003","0005");

function setSelects(){
usertypes=document.getElementById("usertypes");
for(var j=0; j<arrayvalues.length; j++)
for(var i=0; i<usertypes.options.length; i++)
{
if(usertypes.options[i].value==arrayvalues[j])
usertypes.options[i].selected=true;
}
}
window.onload=setSelects;
</script>

<form>
<select id="usertypes" multiple="multiple">
<option value="0033">data1</option>
<option value="0025">data2</option>
<option value="0001">data3</option>
<option value="0003">data4</option>
<option value="1234">data5</option>
<option value="0005">data6</option>
</select>
</form>

However, it appears to work as you want it to (Firefox
1.5.05/Linux/Win2k & IE6).
Maybe the problem is how you are starting the script ?

Being one of those types who prefers to avoid nested loops, let me
suggest a little change to the script:
function setSelects(){
var inverted = new Array();
for(var i=0; i<arrayvalues.length; i++){
inverted[arrayvalues[i]] = true;
}

usertypes=document.getElementById("usertypes");
for(var i=0; i<usertypes.options.length; i++)
{
var curOpt = usertypes.options[i];
curOpt.selected = inverted[curOpt.value];
}
}

Paul:
Thanks for the response. I agree about the nested loops. My
actual code doesnt use nested loops but I wanted to try and describe
the problem in a way that could be more easily understood. Here is my
actual code snippet:

say '<script language="javascript">'
say 'opts=parent.document.getElementById("misc3");'
do zz=1 to xref4s~items
say 'opts.options['xref4s[zz]['FARMTYPEUID']-1'].selected=true;'
end
say '</script>'

xref4s is a server side array where xref4s[1]['FARMTYPEUID']
contains "0001" for example. My form data is passed to a hidden
iframe that contains the above code. The resulting code from the
script will look like:

opts.options[1].selected=true
opts.options[3].selected=true
opts.options[5].selected=true

when my xref4s array contains ("0002", "0004", "0006")

The strange thing is the fact that if I add an alert inside the do
loop then it properly selects all related options.

Ahh, now that is more informative. I suspect that the javascript is
executing before the browser has built the references to the html
components. Using the javascript console on Mozilla/Firefox should
confirm this.
Try wrapping that up in a function called after the page loads.
Paul,
Okay, that did it. By adding the alert it must have given the
script the time it needed to reference the component. After the last
coorespondence I added this code which worked for the time being. It
used the same principle that you were reffering to, by waiting for the
script to complete:

call charout , 'window.onload += '
call charout , 'setTimeout('''
call charout , 'opts=parent.document.getElementById("misc3");'
do zz=1 to xref4s~items
call charout , '
opts.options['xref4s[zz]['FARMTYPEUID']-1'].selected=true;'
end
call charout , ''',100)'
call charout , '0d0a'x

It built a string of the data, and passed it to the setTimeout
function to wait 100 ms. By appending it the window.onload it was
waiting until the load completed before processing the commands. I
really didnt even need the setTimeout function. The end result was as
you suggested. I replaced this with a function call invoked by the
windows onload event. Thanks for the help!
Jul 31 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Rolf Wester | last post: by
6 posts views Thread by Ben Hallert | last post: by
6 posts views Thread by Adam Tilghman | last post: by
2 posts views Thread by areef.islam | last post: by
2 posts views Thread by murraymiken | last post: by
1 post views Thread by abhishekhs | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.