I have the below piece of code that creates a drop down menu in my JSP page. This may seems like a very silly question but how would you preselect say the 4th item in the list so that on start up the JSP would open with the 4th item selected. Thanks very much for any assistance you can give. <%-- Populate the pulldown list of resource titles from the database. --%> <% String[] ResourceTitle = Functions.getTitleList(); if (ResourceTitle != null) { for (int i=0; i < ResourceTitle.length; i++) { if (ResourceTitle.equals (strSelectedResource)) { //If the string in the strSelectedResource is //in the pulldown list use it as the //Preselected option to display. %> <OPTION selected value="<%= ResourceTitle % >"> <%= ResourceTitle %> </OPTION> <% } else { %> <OPTION value="<%= ResourceTitle %>"> <%= ResourceTitle %> </OPTION> <% } } } else { %> <OPTION> No Titles Loaded </OPTION> <% } %> </SELECT> </td>
Jeff Sunder
Ranch Hand
Joined: Jun 26, 2001
Posts: 47
posted
0
I beleive all you have to do is add'SELECTED' to the first block of your if statment:
if (ResourceTitle.equals (strSelectedResource)) { //If the string in the strSelectedResource is //in the pulldown list use it as the //Preselected option to display. %> <OPTION selected value="<%= ResourceTitle % >"> <%= ResourceTitle %> SELECTED</OPTION> <% } else { %> <OPTION value="<%= ResourceTitle %>"> <%= ResourceTitle %> </OPTION> <% }
Dipti Alurkar
Greenhorn
Joined: Jul 05, 2001
Posts: 7
posted
0
I think that you need to add the index of the ResourceTitle array in the 'if'. if (ResourceTitle[i].equals (strSelectedResource)) this might help. the 'SELECTED' word is already included in the <OPTION> tag. Dipti