Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2003
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Select US State (in a var) from the populated state dropdown box

    I thought I had this solved from someone's else's question so I tried the code suggested. I got an error saying the property or method is not supported.

    I have variable containing a state abbreviation.
    I'm trying to get my already-populated drop-down to select the
    state contained in the variable

    document.editForm.state.options[document.editForm.state.selectedIndex].value=s;

    How hard could this be - the state dropdown is already populated and will definitely find the 2-letter abbrev value - isn't there a one-liner to set it?

    Thank you,
    Mary

  • #2
    Regular Coder COBOLdinosaur's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    369
    Thanks
    2
    Thanked 26 Times in 26 Posts
    document.editForm.state.options[document.editForm.state.selectedIndex].value=s;

    Would change the avlue of the currently selected option. I think you want somthing like:

    var obj = document.forms['editForm].state;
    for (i=0;i<obj.options.length;i++)
    {
    if (s==obj.options[i].value)
    {
    obj.options[i].selected=true;
    obj.selectedIndex=i;
    return;
    }
    }
    alert('Stat not found');


    The select options would have the abbreviation as their values:

    <select name="state">
    <option value="VA">Virgina</option>
    <option value="MI">Michigan</option>
    <option value="CO">Colorado</option>

    Etc....
    100% standards compliant code is 100% correct 100% of the time.
    one of my toys from my repository and perhaps some help getting help

    Cd&

  • #3
    New Coder
    Join Date
    Aug 2003
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi - thanks for the reply. I tried both suggestions and neither worked. I can't figure this out.

    I retrieved the state value from the database, "PA" for example, in a variable and simply want to select/highlight "PA" in the state drop down - the abbrev values are already in there and will always find its match. The state drop down looks like this.

    <select name="state">
    <option value=""></option>
    <OPTION VALUE="AB">AB
    <OPTION VALUE="AK">AK
    <OPTION VALUE="AL">AL

    Any further ideas come to mind?
    Thanks!
    Mary

  • #4
    New Coder
    Join Date
    Aug 2003
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    It DID work!

    Sorry about that CDino - it surely did work. I had to remove the return as it was causing an error (outside of function?) and then I removed the alert and now it works beautifully. I suspect, it's not working as efficiently without the return, probably selecting the match, then continuing down the line, but that's okay for now. Thanks a million for your help with this, CDino!
    Best,
    Mary

  • #5
    Regular Coder COBOLdinosaur's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    369
    Thanks
    2
    Thanked 26 Times in 26 Posts
    You are right about it being inefficient. The return should be break:

    var obj = document.forms['editForm].state;
    for (i=0;i<obj.options.length;i++)
    {
    if (s==obj.options[i].value)
    {
    obj.options[i].selected=true;
    obj.selectedIndex=i;
    break;
    }
    }

    sorry about that.
    100% standards compliant code is 100% correct 100% of the time.
    one of my toys from my repository and perhaps some help getting help

    Cd&

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    This is even more efficient - particularly for long lists:

    http://www.oreillynet.com/pub/a/java...p8/index2.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •