All Things Techie With Huge, Unstructured, Intuitive Leaps

JSP - How to Programatically Set the Select of a Combo Box

I had to look this one up, so I am posting this JSP tip in case anyone else needs help. Here is the scenario. I have a form with a select tag and a bunch of options. The data is stored in a data base. When I read the data back and display the page, I want to set the select of the combo box to display the data value of the variable or the database value. It is very easy with a scriptlet. Just add the following attribute code to your option tag:

option value = "Chocolate Brownies" <% if (icecream.equalsIgnoreCase("Chocolate Brownies") out.print("selected"); %>

There you have it. Easy peasy!

No comments:

Post a Comment