Referring to an HTML listbox (multiple select) using JavaScript

Posted on 2004-04-30
Last Modified: 2012-06-27
I have an HTML listbox in an ASP page in which users can select one or more values, as follows:

<select class='cInput' name="listbox" size=5 multiple STYLE='width:300px;'>

On submit a JavaScript function on the same page is called that referrences that listbox as follows:

lcValue =document.forms[formNo].listbox.options[document.forms[formNo].listbox.selectedIndex].value;

formNo is passed into the function.

If I use "var lcValue = String(trim(Request.Form("listbox")));" in another ASP page using POST and more than one value has been highlighted in the box then lcValue automatically returns a list seperated by commas.

However the JavaScript code "document.forms[formNo].listbox.options[document.forms[formNo].listbox.selectedIndex].value" only returns the first selected item in the listbox.

How can I get lcValue to return a comma seperated list in JavaScript?

Question by:kaiwhoeatspie
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
  • 3

Expert Comment

ID: 10961356
Try the following (the last bit removes the trailing comma (,)...

var i
var lcValue
lcValue = ""
for(i=0; i<=document.forms[formNo].listbox.length - 1; i++)
            lcValue = lcValue + document.forms[formNo].listbox.options[i].value + ","
lcValue = lcValue.substr(0,lcValue.length - 1);
LVL 10

Accepted Solution

Bustarooms earned 250 total points
ID: 10961425
function getOptions(sel){
  var opts='';
      if(opts == ''){opts=sel.options[i].value;}
      else{ opts+=',' + sel.options[i].value;}
  return opts;

call it as var IcValue = getOptions(document.forms[formNo].listbox);

Author Comment

ID: 10961508
Thanks, I will try your suggestions on Tuesday (weekend then national holiday in UK on Monday). Sorry that I can't test now.
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now


Author Comment

ID: 10984254
I gave Bustarooms the points because:

a) code is cleaner, function can be reused
b) it only creates a comma seperated list for selected option values, not all of them
LVL 10

Expert Comment

ID: 10985203
ok, why the B?

Author Comment

ID: 10985278
Correct me if I am wrong but I think a B should be given for a question that has been answered correctly and to spec. An A is for a question that has been answered "beyond the call of duty", where extra special effort has been made beyond what was required.

Do you think that is not fair?
LVL 10

Expert Comment

ID: 10985321
to me, any help you get on this board, where someone takes the time to help you out is "beyond the call of duty"

Here's what EE thinks about grading

Author Comment

ID: 10985766
I stand corrected. An "A" it is. I will contact EE and ask them to upgrade.

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

Suggested Solutions

Title # Comments Views Activity
Link Anchor Not working 9 67
Problem in javascript 17 59
Change background images after 5 seconds. 12 48
Switching ids and wanting to access array to output right value 6 21
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…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

751 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