[Javascript] Setting default selection in dropdown

Esther_Strom at hmco.com Esther_Strom at hmco.com
Fri Aug 3 09:12:57 CDT 2001


Peter,

Thanks for the help. This is the track I need, but, as I said, it's been a
while since I've worked with JS, and I'm making stupid mistakes.

Because I need to perform this routine on several cookie/select pairs, I
wanted to turn your code into a function where I could pass in the cookie
and select name. This is what I came up with:

function findMatch(cookieName, selectName){
var i = 0;
var cookieValue = unescape(getCookieValue(cookieName));
alert("Select Name passed: " + selectName);
alert("CookieValue in Match: " + cookieValue);
var mySelect = document.forms[0].selectName;
alert("mySelect: " + mySelect);
while(i < mySelect.options.length) {
  if(mySelect.options[i].value == cookieValue) {
    mySelect.options[i].selected = true;
    }
    i++;
  }
  }

It's being called like this:

findMatch(cookie1, 'Project');

(cookie1 isn't in quotes because I'm calling this function from within
another function, where cookie1 is passed in.) getCookieValue is another
function which works correctly. The line that's causing problems is:

var mySelect = document.forms[0].selectName;

The alert for that one produces "undefined", and I get a javascript error
saying "mySelect has no properties". Is there some other way I should be
appending the selectName to the document.forms[0]. part?


Thanks,

Esther




                                                                                                                       
                    "Peter Brunone"                                                                                    
                    <peter at brunone.co        To:     <javascript at LaTech.edu>                                           
                    m>                       cc:                                                                       
                    Sent by:                 Subject:     Re: [Javascript] Setting default selection in dropdown       
                    javascript-admin@                                                                                  
                    LaTech.edu                                                                                         
                                                                                                                       
                                                                                                                       
                    08/02/01 03:49 PM                                                                                  
                    Please respond to                                                                                  
                    javascript                                                                                         
                                                                                                                       
                                                                                                                       




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
|

_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
http://www.LaTech.edu/mailman/listinfo/javascript







More information about the Javascript mailing list