wood burning stoves*
The moose likes HTML, CSS and JavaScript and the fly likes Getting Multiple Values from a Select Tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Getting Multiple Values from a Select Tag" Watch "Getting Multiple Values from a Select Tag" New topic
Author

Getting Multiple Values from a Select Tag

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1489
    
    1
I need a list that the user can select multiple items in. How do I gather the values of the selected items?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

They'll be marked with the selected attribute.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
loop through and look for the selected attribute.

if you use jQuery, you can use :selected

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

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

Or simple the .val() method
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1489
    
    1
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

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

Joined: Nov 16, 2004
Posts: 1489
    
    1
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.
>
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

the for statement is not getting elements.length
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1489
    
    1
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

Joined: Apr 30, 2003
Posts: 820

Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

I mean


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Have you run the script in Firefox with Firebug enabled?

Some basic debugging will show that your script fails immediately.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1489
    
    1
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Beyond that, your script has some major problems. Are you only testing in IE?
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1489
    
    1
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Answer this question: how can you use getElementById() to find elements that have no id?!
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Nov 16, 2004
Posts: 1489
    
    1
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

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

Joined: Nov 16, 2004
Posts: 1489
    
    1
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Yes, if you are going to use id to reference elements, make sure that you use the id and not the name.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting Multiple Values from a Select Tag