File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes Populating a dependant select list without reloading the page 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 "Populating a dependant select list without reloading the page" Watch "Populating a dependant select list without reloading the page" New topic

Populating a dependant select list without reloading the page

Vanessa Danin
Ranch Hand

Joined: Oct 04, 2004
Posts: 34
I have a JSP which includes 2 select lists. The option selected from the first select list determines which set of options is displayed in the second select list.

The first select list always consists of the same, few options whereas the second select list's options may be numerous and importantly, are dynamic not only in terms of option list dependancy on the first select list but also because the values that make up each possible set of option values for the second select list are based on editable database values and therefore may change.

Currently, when I select an option from the first select list, I submit and reload the page to send through the selected option as a request parameter and use it to set a pageContext attribute in the reloaded page. I then test this pageContext attribute against several 'if' conditions to identify which is the correct set of options to display for the second select list. Then, I iterate through the set of values of the session-scoped attribute which applies to the matched 'if' condition. The session-scoped attribute holds a set of values retrieved from the database which are finally written out to the page as the second select list's option values.

My question is this: is there a way to display the relevant options for the second select list without reloading the page?
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
Only way is with JavaScript by loading all of the options for the second Dropdown into arrays. If you are talking about tons and tons and tons of options then it will cause your page loading time to increase.

More info: see for double combo scripts.

Vanessa Danin
Ranch Hand

Joined: Oct 04, 2004
Posts: 34
Thanks so much for your reply Eric. Yes, I had decided against using Javascript arrays for that reason. I was hoping that there was another alternative but as there does not appear to be one I will continue using my existing code. Thanks once again for taking the time to assist me in making my decision.
Daniel Hedrick

Joined: Aug 08, 2003
Posts: 10
Not to send anyone away from Javaranch, but I recently came across this very useful tidbit that I think might be useful if you have a considerable amount of data to shuffle about:

Good luck!


There are 10 types of people in this world...<br />Those who understand binary, and those who don't.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63849

Moving to the HTML/Javascript forum.

[Asking smart questions] [About Bear] [Books by Bear]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
More involved but possible is to load JUST the select as a separate JSP using the capabilities of javascript to do requests to a webserver and inject the resulting data (html code in this case) into an html element (td or div most likely).
That's what we do sometimes and it works (except for a spurious javascript error which happens sometimes and seems unrelated to the mechanism itself).

The actual code is proprietary and I can't give it to you but the principle isn't that difficult.

Vanessa Danin
Ranch Hand

Joined: Oct 04, 2004
Posts: 34
Thank you very much Daniel and Jeroen. Daniel, I found the article you attached to be very helpful and am currently experimenting with the remote scripting methods discussed as a possible solution to my problem.

The principle of what Jeroen suggests is encompassed by the remote scripting concept.
I agree. Here's the link:
subject: Populating a dependant select list without reloading the page
It's not a secret anymore!