• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to select multiple options in Listbox using Javascript?

 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please cool down. Everybody is trying to help.

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric's code snippet works fine for me .It does not deselct the previous selected ones.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Dinner will be steamed monkey heads with a side of tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic