How to retain value of dropdown list when it changes?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Akino877
    New Member
    • Nov 2007
    • 37

    How to retain value of dropdown list when it changes?

    Hello,

    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 :

    Code:
    	<select name="trophyscope" size="2" onChange="
    	<%String item = (String renderRequest.getPortletSession().getAttribute("trophyscope");
    		%>
    		<option selected="true" value="<%=item%>">"<%=item%>"</option> 
    	        "
    	>
    I wonder if someone could please point me to what I am doing wrong?

    Thank you,

    Akino
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.

    Comment

    • C CSR
      New Member
      • Jan 2012
      • 144

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

      Code:
      <option selected="true" value="<%=item%>"><%=item%></option>

      Comment

      • C CSR
        New Member
        • Jan 2012
        • 144

        #4
        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).

        Comment

        • Akino877
          New Member
          • Nov 2007
          • 37

          #5
          Hello C CSR,

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

          Code:
          <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,

          Akino

          Comment

          • C CSR
            New Member
            • Jan 2012
            • 144

            #6
            Akino:

            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.

            Comment

            • C CSR
              New Member
              • Jan 2012
              • 144

              #7
              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:

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

              Other option are: Blank

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

              OR

              no option selected line at all
              CODE

              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?

              Comment

              • C CSR
                New Member
                • Jan 2012
                • 144

                #8
                I hope the Moderaror doesn't tear up my code. I forgot to put it in the "CODE blocks."

                Comment

                Working...