aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Dynamic Drop Down 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 "Dynamic Drop Down" Watch "Dynamic Drop Down" New topic
Author

Dynamic Drop Down

Keith Harrison
Greenhorn

Joined: Mar 04, 2006
Posts: 15
In a JSP, I would like to know how to dynamically change values in selection box 2, based on selection box 1. All values will be predetermined.

For example
Selection box 1 values, 'House', 'Shop'

Selections in box 2 based on selecting 'House' would be 'Semi Detached', 'Detached', 'Appartment', 'Mansion'

Selections in box 2 based on selecting Shop' would be 'Electrical Store', 'Grocery Store', 'Clothing Store', 'Car Spares'

How would the page refresh once you have chosen a value?

Thanks in advance.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Keith Harrison:
...How would the page refresh once you have chosen a value? ...

Well, it would sure be nice if you didn't need to "refresh" the page (get the server to provide a new version) every time the user picks an option, right?

This sounds like something that would be better if programmed on the client (browser) side, so that the options can change without going back and forth with the server. For that, something like JavaScript would be good.

I'm going to go out on a limb, and move this to the JavaScript forum.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
I am coding this here in this textarea so hopefully there will be no bugs!



Eric
[ August 17, 2006: Message edited by: Eric Pascarello ]
Keith Harrison
Greenhorn

Joined: Mar 04, 2006
Posts: 15
Thats very helpful. Thanks very much Eric.
Keith Harrison
Greenhorn

Joined: Mar 04, 2006
Posts: 15
Referring to the previous post by Eric, does anybody know a way that I can set the selected option if I know its value?

For example, I have a subCategory value that�s being passed back from a Servlet as a request attribute and would like to set the relevant option as selected.

The array is declared as follows:
selectOptions['SHEETS'] = new Array('Choose a sub category','Flat', 'Centre Folded', 'Multi Folded');

On loading the page, I want to assign the option 'Flat' as being selected within the following code:


//Create a new option (text,value)
var newOpt = new Option(selectOptions[selectedItem][i], selectOptions[selectedItem][i]);
//append it to the options list
sel.options[sel.options.length] = newOpt;

Any help or guidance will be appreciated.

Thanks very much.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You can, have to call the function onload and have the servlet set a variable or hidden field with the value that needs to be selected.



change AddOPtions to accept new parameter


change code to see if we should select it


Eric
[ October 30, 2006: Message edited by: Eric Pascarello ]
Keith Harrison
Greenhorn

Joined: Mar 04, 2006
Posts: 15
Thanks Eric.

I can see the logic behind your reply but I cant seem to piece it together. At the beginning of my page I obtain the request attributes:



Then further down my page I have the select objects:



Is there an easier way to set the select1 option instead of doing this:



Once I have set the select1 (category) option, I need to call the onchange AddOption function to fill select2 (subCategory). I then need to call the select2 toggleOther function which already enables or disables other input boxes.

Thanks again.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Keith Harrison:
...
Is there an easier way to set the select1 option instead of doing this:



....


Yes.
If you have to use scriptlets you can do something like..


It can be done more cleanly with JSTL/EL but this should save you a little typing.
[ October 30, 2006: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Keith Harrison
Greenhorn

Joined: Mar 04, 2006
Posts: 15
That�s great thanks Ben, it worked a treat.

Setting select1 using your solution works fine but I want select2 to have a selected option. Eric showed me a way of doing this further up the same post but for some reason its not getting recognised.

<script type="text/javascript">

var selectedValue = "<%=subCategory%>";

window.onloaad = function(){
var sel = document.frmSearch.select1;
var val = sel.options[sel.selectedIndex].value;
AddOptions(val,selectedValue);
}
</script>


What I am finding is when the page loads, the two select objects get populated but select2 always has 'Choose a sub category' as the selected option when 'Flat' is being returned from the servlet.

Any ideas?

Thanks very much.
[ November 01, 2006: Message edited by: Keith Harrison ]
Keith Harrison
Greenhorn

Joined: Mar 04, 2006
Posts: 15
Sorry to bring this one up again Eric but I have tried meddling with this problem numerous times now and cannot find a solution.

The answer may be simple but I cannot fathom it out!

When my page reloads with records returned by a search, Select2 does not have the correct option selected, even though your example does correctly evaluate the right option to be selected. Three options are being created and even though the second option should be selected, the first always gets selected.

Thanks for any help you can offer

Keith.
Neerav Narielwala
Ranch Hand

Joined: Dec 08, 2006
Posts: 106
You can even do it using AJAX... www.fornewbie.com/article/php/create_ajax_dynamic_drop_down_list_using_php_-_xajax.html


<a href="http://www.java-tips.org/java-tutorials/tutorials/" target="_blank" rel="nofollow">Java Tutorials</a> | <a href="http://www.planet-java.org" target="_blank" rel="nofollow">Java Weblog</a> | <a href="http://computer-engineering.science-tips.org" target="_blank" rel="nofollow">Computing Articles</a>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Dynamic Drop Down