Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-24-2003, 07:55 PM   PM User | #1
pa2stepper
New Coder

 
Join Date: Aug 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
pa2stepper is an unknown quantity at this point
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
pa2stepper is offline   Reply With Quote
Old 10-25-2003, 07:22 PM   PM User | #2
COBOLdinosaur
Regular Coder

 
COBOLdinosaur's Avatar
 
Join Date: Jul 2002
Location: Canada
Posts: 293
Thanks: 0
Thanked 18 Times in 18 Posts
COBOLdinosaur is an unknown quantity at this point
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&
COBOLdinosaur is offline   Reply With Quote
Old 10-26-2003, 12:16 AM   PM User | #3
pa2stepper
New Coder

 
Join Date: Aug 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
pa2stepper is an unknown quantity at this point
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
pa2stepper is offline   Reply With Quote
Old 10-26-2003, 12:29 AM   PM User | #4
pa2stepper
New Coder

 
Join Date: Aug 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
pa2stepper is an unknown quantity at this point
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
pa2stepper is offline   Reply With Quote
Old 10-26-2003, 09:51 PM   PM User | #5
COBOLdinosaur
Regular Coder

 
COBOLdinosaur's Avatar
 
Join Date: Jul 2002
Location: Canada
Posts: 293
Thanks: 0
Thanked 18 Times in 18 Posts
COBOLdinosaur is an unknown quantity at this point
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&
COBOLdinosaur is offline   Reply With Quote
Old 10-27-2003, 02:38 AM   PM User | #6
adios
Senior Coder

 
Join Date: Jun 2002
Posts: 1,404
Thanks: 2
Thanked 32 Times in 32 Posts
adios is on a distinguished road
This is even more efficient - particularly for long lists:

http://www.oreillynet.com/pub/a/java...p8/index2.html
adios is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:10 PM.


Advertisement
Log in to turn off these ads.