Click to See Complete Forum and Search --> : Populating one field from another


johnt
05-28-2003, 03:11 PM
Bit of a problem getting the following to work in IE 5.2 on a Mac. The script is supposed to populate a field with comma separated values taken from a multiple select field. Anyone any ideas?

<html>

<head>
<SCRIPT>
function setVal(list,f) {
__result="";
__for (i=0;i<list.options.length;i++) {
____if (list.options[i].selected) {
______result+=list.options[i].text+", ";
____}
__}
__result=result.substring(0,result.length-2);
__f.elements['table.color'].value=result;
}
</SCRIPT>
</head>
<body bgcolor="#ffffff">
<FORM>
<SELECT NAME="colorlist" SIZE=5 MULTIPLE onChange="setVal(this,form)">
<OPTION>Red</OPTION>
<OPTION>Orange</OPTION>
<OPTION>Yellow</OPTION>
<OPTION>Green</OPTION>
<OPTION>Blue</OPTION>
<OPTION>Violet</OPTION>
</SELECT>
<INPUT TYPE=TEXT NAME="table.color">
</FORM>
</body>

</html>

Nevermore
05-28-2003, 03:16 PM
I don't know about a Mac, but on a PC you have to take out the trailing underscores to make it work.

Charles
05-28-2003, 03:56 PM
Of course, this will still fail for the one in ten users that do not use JavaScript.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<SCRIPT type="text/javascript">
function BooleanHash () {this.hash = new Object()}

BooleanHash.prototype.setKey = function (key) {this.hash[key] = true}

BooleanHash.prototype.toString = function () {
var a = new Array();
for (key in this.hash) {a.push(key)};
return a.sort().toString();
}

var optionHash = new BooleanHash();
</SCRIPT>

<FORM action="" onreset="optionHash = new BooleanHash()">
<DIV>
<SELECT NAME="colorlist" SIZE=5 MULTIPLE onChange="optionHash.setKey(this.options[this.selectedIndex].value); this.form.tableColor.value = optionHash">
<OPTION value="Red">Red</OPTION>
<OPTION value="Orange">Orange</OPTION>
<OPTION value="Yellow">Yellow</OPTION>
<OPTION value="Green">Green</OPTION>
<OPTION value="Blue">Blue</OPTION>
<OPTION value="Violet">Violet</OPTION>
</SELECT>
<INPUT TYPE=TEXT NAME="tableColor"><BR>
<INPUT TYPE=SUBMIT>
<INPUT TYPE=RESET>
</DIV>
</FORM>

johnt
05-28-2003, 04:05 PM
The trailing slashes are not in my original code - an error of formatting by this BB.