Solved

Multiple Select

Posted on 1998-10-05
8
178 Views
Last Modified: 2012-05-04
How do I retrieve the number of items and the value of the items selected from a multiple select list box?

ie
<SELECT NAME=MULTI MULIPLE>
<OPTION ...
</SELECT>
0
Comment
Question by:obsrhayden
  • 4
  • 2
  • 2
8 Comments
 
LVL 1

Expert Comment

by:nchalasa062698
ID: 1838358
Got to use Javascript.

can write the code if you want.....
0
 

Author Comment

by:obsrhayden
ID: 1838359
Yes I'd like the code
0
 
LVL 1

Expert Comment

by:nchalasa062698
ID: 1838360
To get the number of selected values , you need to use a
for loop in the javascript to loop thru all of them and count the once that are
selected.

function find_selections(selobj)
{
   var  selectednum =0;
   for (var i=0; i<selobj.length; i++)
    { if(selobj.options[i].selected )
            selectednum++
    }
   return selectednum
}
   
0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 1

Expert Comment

by:nchalasa062698
ID: 1838361
To get the number of selected values , you need to use a
     for loop in the javascript to loop thru all of them and count the once that are
     selected.

     function find_selections(selobj)
     {
        var  selectednum =0;
        for (var i=0; i<selobj.length; i++)
         { if(selobj.options[i].selected )
                 selectednum++
         }
        return selectednum
     }
       
0
 

Author Comment

by:obsrhayden
ID: 1838362
That only answer half the question.  How do I get the values of the selected items.
Let's say a function that returns a string with the items delimited by a |.
0
 
LVL 1

Accepted Solution

by:
nchalasa062698 earned 100 total points
ID: 1838363
function find_selections2(selobj)
          {
             var  astr;
             for (var i=0; i<selobj.length; i++)
              { if(selobj.options[i].selected )
                      astr=astr+' | '+selobj.options[i].value
              }
             return astr
          }
             
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 1838364
here is sample code for you...
I think you might find this easier than the above proposed answer:


Value_Array=new Array(document.selectObjectName.options.length);
var numberSelected=0

function interpret() {

          for (var i=0; i < document.selectObjectName.options.length; i++)
          {
                    if (document.selectObjectName.options[i].selected==true)
                    {
                              Value_Array[i]=document.selectObjectName.options[i].value;
                              numberSelected++;
                    }
          }
}



This function will give you numberSelected for the number selected by user
and Value_Array (range 0..numberSelected-1) for an array of strings containing the values of the selected items.

If you get errors try moving the variable declarations inside the function but it
should work.

this should help you out.

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 1838365
I didn't read the delimited by '|' part but that is easy to fix.

Instead of String Array just declare a string variable and init it to '' by:
string_var_name='';

and where I have
  Value_Array[i]=document.selectObjectName.options[i].value;
replace by
  string_var_name=string_var_name+document.selectObjectName.options[i].value+'|';
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

777 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