GeeCON Prague 2014*
The moose likes HTML, CSS and JavaScript and the fly likes get parameters from a dynamic select 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 "get parameters from a dynamic select" Watch "get parameters from a dynamic select" New topic
Author

get parameters from a dynamic select

David Avrami
Ranch Hand

Joined: Sep 10, 2011
Posts: 30

Hi to all,
i have the following select tag:



i use javascript to add options to the select and allowing the user to change the order of those options.
The thing is i don't know how to send the options to the servlet (or retrieve the options like getParameter("OrderList")),
i need all the option's text parameters (best to return with values also but not necessarily).
I have read a little about JSON, still not sure if i really need it to accomplish this task.
any ideas or suggestions ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

You have not made what you are trying to accomplish clear. And the fact that options are dynamically generated is a red herring. The state of the <select> element at submission time is all that matters. It makes no difference how it got to that state.

With that in mind, care to restate your requirements?

By default, a <select> element will have the value of the selected option (which defaults to its content if no value is provided) passed using the name assigned to the select. What is it that you want that is different from this?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Avrami
Ranch Hand

Joined: Sep 10, 2011
Posts: 30

I need the names and the values of the SELECT.
in my servlet the line:
request.getParameterValues("OrderList");
returns NULL although i do have options in the SELECT. (added by javascript)
so i thought maybe it's because initially the select was empty, therefore asking if there is
a different way to get those names and values.

thank for the fast reply.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

David Avrami wrote:I need the names and the values of the SELECT.

There is only one name associated with a <select>. WHat other names are you asking about?

And, only one option value can be selected (and hence returned) unless the select is marked as multiple, in which case it becomes a list rather than a dropdown.

in my servlet the line:
request.getParameterValues("OrderList");
returns NULL although i do have options in the SELECT. (added by javascript)

Is one of the options marked as selected?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

I'm also a little skeptical that your <option> elements are being added correctly. Even with no option selected, the select should default to the first option.

Have you used a DOM inspector to make sure that the HTML DOM is really that you think it is at the time of submission?
David Avrami
Ranch Hand

Joined: Sep 10, 2011
Posts: 30

i need the options names and values like if i have a select like:

<select name="OrderList" size="8" id="OrderList" style="direction:rtl; position:absolute;left:500px;top:200px;width:150px;font-family:Courier New;font-size:16px;z-index:32">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
</select>

so in my servlet i could get a List or other object that will contain:
volvo, Volvo
saab, Saab
mercedes, Mercedes

there can be one selected option but i need them all.
maybe one way do to so is to mark the SELECT as multiple and after the user presses the submit button javascript will select all the options and then submit the form to the server?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

Nope, you will not be able to get the text content (they're not names, so stop calling them that, it's just confusing) of the options using the select element in any configuration -- the text content of the options is not ever part of the submission. If the values are omitted, the values will default from the text content, but that only works if they're to be the same which is not the case here.

If you want all the options returned, including text content and values, you'll need to create hidden inputs containing the information you want.

You could do this in a submit handler established on the form.
David Avrami
Ranch Hand

Joined: Sep 10, 2011
Posts: 30

Hi,
I made a hidden input containing the information i needed in its value.
works great for me.

the hidden input in the html:


the javascript:


thank you for your help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

 
GeeCON Prague 2014
 
subject: get parameters from a dynamic select