[Javascript] Setting default selection in dropdown

Peter Brunone peter at brunone.com
Thu Aug 2 15:49:14 CDT 2001


Esther,

    Just iterate through the options until you get to the correct value,
e.g.

var i = 0;
var cookieValue = "boogawooga"; // or whatever

var mySelect = document.formName.selectName;

while(i < mySelect.options.length) {
  if(mySelect.options[i].value == cookieValue) {
    mySelect.options[i].selected = true;
    }
    i++;
  }

    You can substitute text for value if you want; either one works just
fine.

Cheers,

Peter



----- Original Message -----
From: "Esther Strom" <emstrom at interaccess.com>
To: <Javascript at LaTech.edu>
Sent: Thursday, August 02, 2001 3:26 PM
Subject: [Javascript] Setting default selection in dropdown


| Hi, everyone. I'm new to this group (I was an active member of the
| list several years ago, but got away from JS for a while.) Anyway,
| here's the problem. I have several menus (selects) on a form. I save
| the last used selection to a cookie when the user leaves or shuts
| down. I need to somehow set the default selected item to the cookie
| value when the page is opened again.
|
| I can read the cookie value back with no problem, but how do I set the
| default selected value without knowing the selectedIndex of the
| cookie? (I thought of saving the selectedIndex number to the cookie
| too, but because the dropdown select is populated from a database,
| there may be new records added between the time the cookie is saved
| and when it's accessed. This will make the selectedIndex # wrong.)
|
| Any ideas?
|
| Thanks,
|
| Esther Strom
|
| _______________________________________________
| Javascript mailing list
| Javascript at LaTech.edu
| http://www.LaTech.edu/mailman/listinfo/javascript
|




More information about the Javascript mailing list