Solved

Multiple Select

Posted on 1998-10-05
8
173 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
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…

706 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

18 Experts available now in Live!

Get 1:1 Help Now