By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

How to retain value of dropdown list when it changes?

P: 37

I have a drop-down list that I would like for it to retain the selected value (but it's not working). The following is my code :

Expand|Select|Wrap|Line Numbers
  1.     <select name="trophyscope" size="2" onChange="
  2.     <%String item = (String renderRequest.getPortletSession().getAttribute("trophyscope");
  3.         %>
  4.         <option selected="true" value="<%=item%>">"<%=item%>"</option> 
  5.             "
  6.     > 
I wonder if someone could please point me to what I am doing wrong?

Thank you,

Jan 20 '12 #1
Share this Question
Share on Google+
7 Replies

Expert Mod 5K+
P: 8,639
since I donít know that programming I can only answer in general. you need to set that option element as selected, which has the passed value.
Jan 20 '12 #2

P: 144
Compared to my stuff, you've got some xtra "" in the value= attribute. See if this fixes it:

Expand|Select|Wrap|Line Numbers
  1. <option selected="true" value="<%=item%>"><%=item%></option>
Jan 21 '12 #3

P: 144
Actually, its not in the attribute itself. Its what's in between your start and finishing option tags. (Minor mistake in my description--just look at the example).
Jan 21 '12 #4

P: 37
Hello C CSR,

Thank you so much for your reply. Removing the extra " :

Expand|Select|Wrap|Line Numbers
  1. <option selected="true" value="<%=item%>"><%=item%></option> 
allows my code to work. But it adds an extra item to my drop down list. I wonder if you or someone could please tell me how to set
the selected item on a drop down list without adding a new item to
the list?

Thank you again,

Jan 23 '12 #5

P: 144

Post a larger section of your code (from <Form to /Form> if you can). Meanwhile I'll look at mine again to see how I avoided that. I wrote mine originally a while back and I've just been cut&pasting ever since.
Jan 23 '12 #6

P: 144
If I remove the "option selected" line altogether, I get a blank in the combo before I click the arrow (the same if I leave the line in but put nothing between the >< , regardless of the value= attribute). I find that no matter whether the initial appearance is blank or contains some text, I always see it duplicated (highlighted) at the top of the dropdown when I click (even the blank line) but that's to indicate the current selection if the user doesn't choose something else. This by nature, but does not refer to your question about the duplicate highlighted item along with the same item being shown further down in the list.

What I did in some cases, is put some text in there that is not actually a searchable item, like "Select" for example, and leave my default value in the "option selected" line a preferred value from my list. This way the value is there but what you see is "Select." Then when you click the arrow, all the items in the list appear once, BUT, in keeping with the nature of the combo, the text "Select" is highlighted at the top in the drop list.

So, you either have a highlighted blank line at the top after you click, with nothing showing in the box before you click, or you have "Select" highlighted at the top after you click. But in the list of "true" items, nothing is duplicated. My workaround, to demonstrate, is as follows:

Valueless text:
<option selected value="<%=item%>"><%Response.write "Select"%></option>

Other option are: Blank

<option selected value="<%=item%>"></option>


no option selected line at all

I don't think you can eliminate the highlighted duplication of what's already in the combo before you click. Ergo, do you want the combo to first appear blank, put some text in there like "Select item," OR, the 3rd option is to eliminate the "option selected" line completely; then, the first item in your recordset for the list will automatically appear (depending on how you were sorted) , but you still have the duplicate in the highlight, but in the highlight ONLY.

Did you follow all that?
Jan 23 '12 #7

P: 144
I hope the Moderaror doesn't tear up my code. I forgot to put it in the "CODE blocks."
Jan 23 '12 #8

Post your reply

Sign in to post your reply or Sign up for a free account.