• 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

Getting Multiple Values from a Select Tag

 
Ranch Hand
Posts: 2211
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need a list that the user can select multiple items in. How do I gather the values of the selected items?

 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They'll be marked with the selected attribute.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
loop through and look for the selected attribute.

if you use jQuery, you can use :selected

Eric
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eric Pascarello wrote:if you use jQuery, you can use :selected


Or simple the .val() method
 
Steve Dyke
Ranch Hand
Posts: 2211
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:They'll be marked with the selected attribute.



I am only getting the last value selected.

My JSP:



My JS:

 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't show JSP markup in the HTML forum. Rather, post the HTML generated from the JSP.

Check your script. Is it really inspecting the correct elements?
 
Steve Dyke
Ranch Hand
Posts: 2211
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Please don't show JSP markup in the HTML forum. Rather, post the HTML generated from the JSP.

Check your script. Is it really inspecting the correct elements?



Can you look at this and tell me whats wrong? It still will not work.
>
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the for statement is not getting elements.length
 
Steve Dyke
Ranch Hand
Posts: 2211
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim McGuire wrote:the for statement is not getting elements.length



Have changed it to the following but still only get last value.

>
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean


 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you run the script in Firefox with Firebug enabled?

Some basic debugging will show that your script fails immediately.
 
Steve Dyke
Ranch Hand
Posts: 2211
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Have you run the script in Firefox with Firebug enabled?

Some basic debugging will show that your script fails immediately.



I was under the impression that I could just click each item I wanted in my multiple select. I did not realize that the selecting process reqiured the CTRL button to be included to genererate the selection list.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beyond that, your script has some major problems. Are you only testing in IE?
 
Steve Dyke
Ranch Hand
Posts: 2211
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Beyond that, your script has some major problems. Are you only testing in IE?



Yes, only in IE because it's the only browser we use. This is an intranet app developed using Websphere. I usually get an error indication when JS is wrong but I am getting no page error on this issuse.

Can you tell me what is wrong?
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer this question: how can you use getElementById() to find elements that have no id?!
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I love when we only develop for IE, nothing like shooting yourself in the foot for the future. That is why we are in this IE only mess!

document.getElementById("tsonotelist");


get Element By Id, you have no id.

Eric
 
Steve Dyke
Ranch Hand
Posts: 2211
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Answer this question: how can you use getElementById() to find elements that have no id?!



Thats a good question. However, I have hundreds of these bogus lines of code that seem to do what I expect them to do. Are these not supposed to work in IE or IE only?
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that IE has these weird ideas of how things should work that doesn't match the standards that all other browsers use.

This is one case: getElementById() will match an element by its name. That's non-standard behavior and will not work in any other browser. Name and id are distinct and separate concepts.
 
Steve Dyke
Ranch Hand
Posts: 2211
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:The problem is that IE has these weird ideas of how things should work that doesn't match the standards that all other browsers use.

This is one case: getElementById() will match an element by its name. That's non-standard behavior and will not work in any other browser. Name and id are distinct and separate concepts.



Thanks for helping me as grope in the darkness. I am really tring to learn and attempt to be a good programer.
I try to take your suggestions and apply them.

So the fix on my part is make sure my HTML elements all have id attributes, right.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if you are going to use id to reference elements, make sure that you use the id and not the name.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic