473,385 Members | 1,324 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,385 software developers and data experts.

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 2428

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rolf Wester | last post by:
Hi, I have a form with a select element with multiple="true". When using the GET method (I suppose the same happens with the POST method) I can seen that the form sends channels=CH1&channels=CH2...
6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
6
by: Adam Tilghman | last post by:
Hi all, I have found that IE doesn't seem to respect the <SELECT> "multiple" attribute when set using DOM methods, although the attribute/property seems to exist and is updated properly. Those...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
2
by: murraymiken | last post by:
I'm looking to have multiple multiple-select-boxes on a page. But I can only get the contents from the last selected value within a box, via PHP. I've tried numerous methods. What am I doing...
1
by: abhishekhs | last post by:
Hi all I have more than one multiple select lists in a page. Something like this <tr> <td> <select NAME="StrainList" ID="StrainList" SIZE="5" multiple="multiple" style="width: 150px"> <?...
92
by: bonneylake | last post by:
Hey Everyone, Well i was hoping someone could explain the best way i could go about this. i have a few ideas on how i could go about this but i am just not sure if it would work. Right now i...
58
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also...
482
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if this is more of a coldfusion problem or a javscript problem. So if i asked my question in the wrong section let me know an all move it to the correct place. ...
6
by: phpnewbie26 | last post by:
My current form has one multiple select drop down menu as well as few other drop down menus that are single select. Originally I had it so that the multiple select menu was first, but this created...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.