Solved

get multiple selected  values

Posted on 2002-05-13
2
5,317 Views
Last Modified: 2012-05-04
I have a form that has two select boxes with lists of countries. the user can select multiple countries from one box and move them to the other. when they get moved however I need to do some processing on the server side so I created code much like the following example with a javascript function that is supposed to copy the values from the selected items in the box and create a commadilimited list that I can copy into the hidden "Countries" input box on the neccessary form. However I don't know how to create the comma delimited list of selected items. here is what I have (which returns the index position of the first selected item instead of a coma delimited list of all the selected options values)




<script language="JavaScript">
function AddCountries()
{
     var Countries = document.frmSaveDistributionList.unmappedCountriesIds.selectedIndex;
     document.frmAddCountries.Countries.value = Countries;
     document.frmAddCountries.submit();
}
function RemoveCountries()
{
     var Countries = document.frmSaveDistributionList.mappedCountriesIds.selectedIndex;
     document.frmRemoveCountries.Countries.value = Countries;
     document.frmRemoveCountries.submit();
}
</script>

<FORM NAME="frmSaveDistributionList" ACTION="/HMCClubCentral/Index.cfm" METHOD=POST>
<table>
     <tr>
          <td>
               <select multiple name="unmappedCountriesIds" size="7" width="340" style="width=340">
                    <option value="209">South Africa</option>
                    <option value="208">New Caledonia</option>
                    <option value="207">United Kingdom</option>
               </select>
          </td>
          <td align="center" valign="top">
               <a href="" onClick="AddCountries(); return false;">
               <img src="Viewers/Images/add.gif" width="47" height="19" alt="Move the Highlighted Subject to the Choice Box" border="0">
               </a>
               <BR>
               <a href="" onClick="RemoveCountries(); return false;">
               <img src="Viewers/Images/remove.gif" width="67" height="19" alt="Remove the Highlighted Subject from the Choice Box" border="0">
               </a>
          </td>
          <td align="left" valign="top">
               <select multiple name="mappedCountriesIds" size="7" width="340" style="width=340">
                    <option value="204">Taiwan</option>
               </select>
          </td>
     </tr>
</table>

<form name="frmAddCountries" action="/HMCClubCentral/Index.cfm" method="post">
<input type="Hidden" name="dlfrmAction" value="AddCountries">
<input type="Hidden" name="Countries" value="">
</form>
<form name="frmRemoveCountries" action="/HMCClubCentral/Index.cfm" method="post">
<input type="Hidden" name="dlfrmAction" value="RemoveCountries">
<input type="Hidden" name="Countries" value="">
</form>
0
Comment
Question by:SBennett
2 Comments
 
LVL 30

Accepted Solution

by:
third earned 100 total points
ID: 7007512

<SCRIPT>
function getSelectedValues (select) {
  var r = new Array();
  for (var i = 0; i < select.options.length; i++)
    if (select.options[i].selected)
      r[r.length] = select.options[i].value;
  return r;
}

function getSelectedTexts (select) {
  var r = new Array();
  for (var i = 0; i < select.options.length; i++)
    if (select.options[i].selected)
      r[r.length] = select.options[i].text;
  return r;
}

function getSelectedIndices (select) {
  var r = new Array();
  for (var i = 0; i < select.options.length; i++)
    if (select.options[i].selected)
      r[r.length] = i;
  return r;
}

function AddCountries(){
  var Countries = getSelectedValues(document.frmSaveDistributionList.unmappedCountriesIds);
  alert(Countries);
  document.frmAddCountries.Countries.value = Countries;
  document.frmAddCountries.submit();
}

function RemoveCountries(){
  var Countries = getSelectedValues(document.frmSaveDistributionList.mappedCountriesIds);
  alert(Countries);
  document.frmRemoveCountries.Countries.value = Countries;
  document.frmRemoveCountries.submit();
}

</SCRIPT>

the other two functions are optional (getSelectedTexts and getSelectedIndices). it might be also useful for you if you want to return the selected texts or indices.
0
 
LVL 14

Author Comment

by:SBennett
ID: 7009205
Thanks that worked perfectly
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now