Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to select multiple options in Listbox using Javascript?

 
Anirvan Majumdar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to loop the box to do it


Eric
 
Anirvan Majumdar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anirvan Majumdar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please cool down. Everybody is trying to help.

 
Ram Gokul
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric's code snippet works fine for me .It does not deselct the previous selected ones.
 
natasa jones
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic