We help IT Professionals succeed at work.

Javascript help retaining correct drop down list description

cofcmgr
cofcmgr asked
on
185 Views
Last Modified: 2013-11-05
I need to be able to retain the selected text in the drop down list.  For example:  The drop down list default is "Choose a product to manage".  When I select "Enterprise" I want to be able to retain "Enterprise" instead of reverting back to "Choose a product to manage" description when the page refreshes with new content.  The javascript and form are as:

<script language="javascript">
function nav(){
   var w = document.Products.Global.selectedIndex;
   var url_add = document.Products.Global.options[w].value;
   window.location.href = url_add;  
   }
</script>
</head>

<body>
<form style="margin-top: 0px;" name="Products">
<select id="Global" name="Global" onchange="nav()" class="text" style="font-size: 10px;">
            <option value="1">Choose a product to manage</option>
            <option value="?hilite=WSE">Enterprise</option>
            <option value="?hilite=WSX">Express</option>
            <option value="?hilite=WSS">Security</option>
            <option value="?hilite=CPS">Content</option>
</select>
</form>
</body>
Comment
Watch Question

I am unfamiliar with Java coding, but in VB, I would do it on server side.

On post back get the value that is selected and then when the page is refreshed set the drop down list to the value you saved as part of the rpoutine that is run.

On post back:

'/// save the value that exists before post back ///
strSelection = ddlManagement.SelectedItem.Text

'/// RUN WHATEVER CODE HERE ///

'/// then as part of the above code before post back, reset the drop down list ///
'/// again, this is in VB.NET but the idea is the same ///
ddlManagament.SelectedItem.Selected = false
ddlManagament.Items.FindByText(strSelection).Selected = true

The idea is the same, but since Java does not have a built in retaining of post back values simply code it into the routine.

Hope that this helps

John
CERTIFIED EXPERT
Top Expert 2006

Commented:
<script language="javascript">
function nav(){
   var w = document.Products.Global.selectedIndex;
   var url_add = document.Products.Global.options[w].value;
   window.location.href = url_add;  
   }
</script>
</head>

<body>
<form style="margin-top: 0px;" name="Products">
<select id="Global" name="Global" onchange="nav()" class="text" style="font-size: 10px;">
            <option value="1">Choose a product to manage</option>
            <option value="?hilite=WSE">Enterprise</option>
            <option value="?hilite=WSX">Express</option>
            <option value="?hilite=WSS">Security</option>
            <option value="?hilite=CPS">Content</option>
</select>
</form>

<script language="javascript">
function PopulateCurrentSelection() {
   var opts = document.Products.Global.options;
   var i;
   for (i = 0 ; i < opts.length ; i++) {
      if (opts[i].value == window.location.href) {
          opts[i].selected = true;
          break;
      }
   }
}

PopulateCurrentSelection();
</script>

</body>

Author

Commented:
Amit,

Thank you...I pasted the code you had written in my browser but still rendered the same results.  Can you be more explicit with instructions?  I am a novice with using javascipt.  Thanks again!
CERTIFIED EXPERT
Top Expert 2006
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.