*
The moose likes HTML, CSS and JavaScript and the fly likes How to select multiple options in Listbox using Javascript? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to select multiple options in Listbox using Javascript?" Watch "How to select multiple options in Listbox using Javascript?" New topic
Author

How to select multiple options in Listbox using Javascript?

Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Hi,
I need to select multiple options in a listbox using javascript. If say, i have 3 values to be selected then using Form.listBox.selectedIndex only lets the last value to be selected. The other two remain un-selected.

Can anyone let me know how to circumvent this problem. Really appreciate any help.
Thanks!
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
you need to loop the box to do it


Eric
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
I have already tried the looping and selecting method, but see, if you've got say 3 options, then only the last one remains selected. The first two are not!

It somehow treats it the same as a dropdown. Like in a dropdown you cannot select more than one option at a time. Afterall, if you come to think of it, listbox is but the "select" tag having the attribute "size" > 1.

Anyone else got something which might work?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15289
    
    6

Originally posted by Anirvan Majumdar:
Afterall, if you come to think of it, listbox is but the "select" tag having the attribute "size" > 1.


Not exactly. You need to tell the select that it allows multiple selections.


GenRocket - Experts at Building Test Data
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Dude....
see, the "HTML" code i wrote to declare the listbox was:


Now say you need to write a javascript method which would select and highlight 2,4 and 5 option, TOGETHER. How to go about that??

If you use say, ".selected" then only the last one is selected. If you use, ".selectedIndex" then also only the last one set remains highlighted.

My question is, IS THERE ANY WAY TO SELECT ALL OF THEM TOGETHER USING JAVASCRIPT? .... i hope i've mde it seem much clearer this time round atleast.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Please cool down. Everybody is trying to help.



[My Blog]
All roads lead to JavaRanch
Ram Gokul
Ranch Hand

Joined: Oct 07, 2005
Posts: 85
Eric's code snippet works fine for me .It does not deselct the previous selected ones.
natasa jones
Ranch Hand

Joined: Aug 20, 2006
Posts: 45
i think this link is the answer to what you need..
http://www.w3schools.com/js/tryit.asp?filename=try_dom_select_multiple
Juan Carlos Marcano
Greenhorn

Joined: Dec 18, 2013
Posts: 1
Anirvan Majumdar wrote:Dude....
see, the "HTML" code i wrote to declare the listbox was:


Now say you need to write a javascript method which would select and highlight 2,4 and 5 option, TOGETHER. How to go about that??

If you use say, ".selected" then only the last one is selected. If you use, ".selectedIndex" then also only the last one set remains highlighted.

My question is, IS THERE ANY WAY TO SELECT ALL OF THEM TOGETHER USING JAVASCRIPT? .... i hope i've mde it seem much clearer this time round atleast.




Use .setAttibute("Selected","selected"); its works.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to select multiple options in Listbox using Javascript?
 
Similar Threads
re:selecting an option from a select box
Listbox Transfer
JSP beginner - adding listbox
help on html: options
code to retrieve result from databse which are selected in the list box