Click to See Complete Forum and Search --> : Populating a multiple select field
johnt
05-01-2003, 03:47 PM
I need to populate a multiple select field with values generated from a database. Unfortunately, the data values appear as "blue or green or blue" (as an example). The following script has been suggested to me but it doesn't seem to work. I have added in the value exactly as it is generated by my database - ie Features or Freelance and the multiple select field name is jobs.jobcatory:
<script language="JavaScript">
function resetSelect(){
val=unescape("Features%20or%20Freelance");
parts=val.split(' or ');
for (i=0;i<parts.length;i++) {
for (j=0;j<document.forms[0].elements['jobs.jobcategory'].options.length;j++) {
if (parts[i]==document.forms[0].elements['jobs.jobcategory'].options[j].text) {
document.forms[0].elements['jobs.jobcategory'].options[j].selected=true;
}
}
}
}
</script>
</head>
<body onload="resetSelect()">
Any suggestions greatly appreciated.
From my understanding of what you are trying to do, you are going to need to use some server side language to write these values in. Something like this, with PHP...
<select name="myselect">
<option value="<? echo $somevar; ?>"><? echo $somevar; ?></option>
<option value="<? echo $someothervar; ?>"><? echo $someothervar; ?></option>
</select>
johnt
05-01-2003, 04:30 PM
Sorry didn't make myself clear. I need to use Javascript to make selections in a multiple select field (based on a series of values that have been separated by ' or ') rather than populate it with just the values. This is the multiple select field in question:
<select name="jobs.jobcategory" size="6" multiple>
<option value="Art and design">Art and design</option>
<option value="Editorial assistants and trainees">Editorial assistants and trainees</option>
<option value="Editors and management">Editors and management</option>
<option value="Features">Features</option>
<option value="Freelance">Freelance</option>
<option value="News">News</option>
<option value="Photographers">Photographers</option>
<option value="Picture researchers">Picture researchers</option>
<option value="Production and sub-editors">Production and sub-editors</option>
<option value="Proofreaders">Proofreaders</option>
<option value="Public relations">Public relations</option>
<option value="Section editors">Section editors</option>
<option value="Specialist writers">Specialist writers</option>
</select>
cbVision
10-28-2009, 01:14 PM
BUMP
I too can't seem to figure out how to populate a multiple select field.
Say I have a multiple select with the options of:
1. red
2. green
3. blue
4. yellow
When the form loads I want to specify the ID's of which colors to multi-select. If I set it to 1 (red), then 4 (yellow), it overrides the red and just selects yellow.
Any ideas?
JMRKER
10-28-2009, 06:49 PM
Is this more like what you want to do?
<html>
<head>
<title>Multiple Selection Drop Down</title>
<script type="text/javascript">
function SetMDDsize(mddSize) {
document.getElementById('MultDD').size = mddSize;
}
function GetMDDselections() {
document.getElementById('MultDD').size = 1; // reduce screen size
var picked = '';
for (i=0; i<document.getElementById('MultDD').options.length; i++) {
if (document.getElementById('MultDD').options[i].selected == true) { picked += i+','; }
}
document.getElementById('MultDD').selectedIndex = -1; // reset list
return picked;
}
</script>
</head>
<body>
Click, Shift-Click or Ctrl-Click<br />
<select multiple id="MultDD" size="1" onClick="SetMDDsize(10)">
<option value="">-Select-</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<button id="MultDDbtn" onClick="alert(GetMDDselections())">Get Selections</button>
</body>
</html>
If not, please explain further ...
Good Luck!
:)
rnd me
10-28-2009, 07:42 PM
BUMP
I too can't seem to figure out how to populate a multiple select field.
Say I have a multiple select with the options of:
1. red
2. green
3. blue
4. yellow
When the form loads I want to specify the ID's of which colors to multi-select. If I set it to 1 (red), then 4 (yellow), it overrides the red and just selects yellow.
Any ideas?
1. make sure to use "multiple" attrib on select.
2. are you using .selectedIndex or option.selected ?