Solved

Multiple Select

Posted on 1998-10-05
8
181 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
Technology Partners: 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!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
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 an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

733 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