Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selectbox - pass multiple selected values to a text box

Status
Not open for further replies.

vangundy

Programmer
Jan 29, 2005
38
CA
Is there a way to add the selected items (could be multiple) from alistbox to a text box sperated by ;?

 
This quick example has what you need.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="Tracy Dryden">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script language="JavaScript" type="text/javascript">
function moveEm() {
  var sSelected = "";
  var theSel = document.forms('myForm').elements('mySel');
  for ( var x = 0; x < theSel.options.length; x++ ) {
    if ( theSel.options(x).selected ) {
      sSelected += ( sSelected == "" ? "" : ";" );
      sSelected += theSel.options(x).value;
    }
  }
  document.forms('myForm').elements('myBox').value = sSelected;
}
function clearIt() {
  document.forms('myForm').elements('myBox').value = "";
  document.forms('myForm').elements('mySel').selectedIndex = -1;
} 
</script></head>
<body>
<div align="center">
<form name="myForm">
<select name=mySel size=5 multiple>
  <option value="red">red</option>
  <option value="green">green</option>
  <option value="blue">blue</option>
  <option value="orange">orange</option>
  <option value="white">white</option>
  <option value="black">black</option>
  <option value="yellow">yellow</option>
  <option value="purple">purple</option>
  <option value="gray">gray</option>
  <option value="brown">brown</option>
  <option value="turquoise">turquoise</option>
</select>
<br>
<input type=text name="myBox" size=50 value="">
<br>
<input type=button onclick="moveEm()" value="Move Em">&nbsp;<input type=button onclick="clearIt()" value="Reset">
</body>
</html>


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Would you believe that code works? I wrote that code, ran it to make sure it works, then just cut&pasted the whole thing here. You're right about the square brackets though. I've been switching back and forth between VB and Javascript lately and constantly get the two mixed up syntax-wise.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Yeah, that's what I meant. It's such a simple program that it didn't seem worth testing in multiple browsers, and I didn't expect to make such a stupid error, and I REALLY didn't expect IE to let it go if I DID make a stupid error.
I'll have to remember that little "gothca".


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
This is great! My actual listbox passes all selected items to another listbox. Is there a way to delete all the items in the second listbox???

Other than that the code or your sample is perfect!
 
Try this:
Code:
function clearListbox() {
  var theSel = document.forms['myForm'].elements['mySel'];
  for ( var x = theSel.options.length-1; x >= 0; x-- ) {
    theSel.remove(x);
  }
}

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Or simply this?
[tt] theSel.options.length=0[/tt]
 
Does that actually work tsuji? If so that's perfect! I was looking for a removeAll method and didn't find one.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Tracy,

It would. But, it is rather a surprised, I must say. If one not working on select options for a good while, it is most easily forgotten because it deviates normal way of thinking and it is some sort of special rule for it. I remember I once in a brain-code post removing options with the same logic you are using, then I think mwolf came up with .length=0, I immediately realized that it is a valid tweak that did not come straight to my mind.

It is so strange. The length "should" be a read-only property. But then the implementation and standards make it read-write. That's a bit crazy I must say.
[tt]
<html>
<head>
</head>
<body>
<form name="myForm">
<select name=mySel size=5 multiple>
<option value="red">red</option>
<option value="green">green</option>
<option value="blue">blue</option>
<option value="orange">orange</option>
<option value="white">white</option>
<option value="black">black</option>
<option value="yellow">yellow</option>
<option value="purple">purple</option>
<option value="gray">gray</option>
<option value="brown">brown</option>
<option value="turquoise">turquoise</option>
</select>
</form>
<button onclick="document.myForm.mySel.options.length=0;">remove all options</button>
</body>
</html>
[/tt]
regards - tsuji
 
You're right, it is somewhat counter-intuitive. But as long as it works ...

I'll have to make a note of that so I'll remember it next time I need it.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Setting the length to 0 also works with arrays, and no doubt collections, too. Put this in your URL bar and run for a quick example:

Code:
javascript:var myArray = [1, 2, 3, 4, 5]; alert(myArray.length); alert(myArray); myArray.length = 2; alert(myArray);

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
I think what the awkward feeling is from analogy of putting .length to containers or elements:
[tt]document.forms.length=0;[/tt] //fictitious
to eliminate all the form container which will sure not do.
- tsuji
 
You're quite welcome. Glad to be of help.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top