Solved

Multiple Select

Posted on 1998-10-05
8
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

690 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