|
I have a page that calls a pop-window with a Listview control enabled for multiple Selections. I don't know how many selections the user will make. I would like to pass this info back to the calling page. THanks!
|
|
|
I am assuming that since you posted in the ASP forum, that by "Listview" you mean a multi <SELECT>.
Copy and paste this code into a new HTML file, then double-click it. Select 1 to 5 cars, then click "Submit". You will see a javascript alert that shows you which cars you selected.
<script language=javascript>
function ReturnCars()
{
var selectedCars;
var e = document.forms[0].cars;
for(i=0;i<e.length;i++)
{
if(e.selected)
{
if(!selectedCars)
{
selectedCars = e.text;
}
else
{
selectedCars += "," + e.text;
}
}
}
alert(selectedCars);
}
</script>
<form method=post action="/forum/MultiSelect.html">
<select name=cars multiple size=5>
<option>Ford</option>
<option>Chevy</option>
<option>Toyoya</option>
<option>Dodge</option>
<option>BMW</option>
</select>
<br>
<input type=button value="Submit" onclick=ReturnCars()>
</form>
|
|
Now all you have to do is instead of popping up an alert, send the comma-seperated list of cars back to a function on the parent page, then close the popup.
self.opener.SomeFunction(selectedCars);
self.close();
|
|
<Added>
I've done it again! I commonly use the variable "i" for an index. In javascript, you commonly place the index between square brackets. Well, in this CodeToad forum, an i between square brackets means italics! DOH! So my code example got corrupted. Below, I show the code again, this time using "idx" as my index variable.
<script language=javascript>
function ReturnCars()
{
var selectedCars;
var e = document.forms[0].cars;
for(idx=0;idx<e.length;idx++)
{
if(e[idx].selected)
{
if(!selectedCars)
{
selectedCars = e[idx].text;
}
else
{
selectedCars += "," + e[idx].text;
}
}
}
alert(selectedCars);
}
</script>
<form method=post action="/forum/MultiSelect.html">
<select name=cars multiple size=5>
<option>Ford</option>
<option>Chevy</option>
<option>Toyoya</option>
<option>Dodge</option>
<option>BMW</option>
</select>
<br>
<input type=button value="Submit" onclick=ReturnCars()>
</form>
|
|
|
|
|
|
|
|
|
|
|
|