[Javascript] Setting default selection in dropdown

Esther_Strom at hmco.com Esther_Strom at hmco.com
Fri Aug 3 10:29:43 CDT 2001


Thanks so much! That did it...told you I was rusty!

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/03/01 10:24 AM                                                                                  
                    Please respond to                                                                                  
                    javascript                                                                                         
                                                                                                                       
                                                                                                                       




Esther,

    If you're passing in a name, you'll want to build the object reference
and eval() it, e.g.

var mySelect = eval('document.forms[0].' + selectName);

    Otherwise you're telling the JS engine that there is a select dropdown
named selectName, which is even more confusing since it's a function
parameter :)

Cheers,

Peter

----- Original Message -----
From: <Esther_Strom at hmco.com>
To: <javascript at LaTech.edu>
Sent: Friday, August 03, 2001 9:12 AM
Subject: Re: [Javascript] Setting default selection in dropdown


|
| 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
|
|
|
|
| _______________________________________________
| 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