Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RE: Passing pre-selected values to a pulldown menu

Status
Not open for further replies.

FreeBASE

Programmer
May 3, 2002
39
US
basically I have a pre-select value, lets say "AK"
I want the pulldown to reflect the value chosen.

So if I use the value of "AL" the pulldown will say "Alaska"


I have a pulldown menu that looks like this below:

<script language=&quot;javascript&quot;>
<!--//
function FillOptionsOptions() {
document.theForm.country.selectedIndex = 2; // selects the 2nd value of the pulldown, this works
document.theForm.country.selectedIndex = &quot;AK&quot;; // should select the &quot;CA&quot; value of the pulldown, doesn't work
}
//-->
</script>

<body onload=FillOptions()>

<form name=theForm>
<select name=country>
<option value=&quot;AL&quot;>Alabama
<option value=&quot;AK&quot;>Alaska
<option value=&quot;AS&quot;>Am. Samoa
</select>

</body>

Could someone please help me on this one..?
 
hi Nuxy,

the selectedIndex property only works for exactly that: the selectedIndex. if you want to test for a selectedIndex's value or text, you can use this syntax:
[tt]
var sel = document.theForm.country;
if (sel.options[sel.selectedIndex].text == &quot;AK&quot;) {
//do stuff here...
}

value[/tt] property is for whatever is in the <option>'s &quot;value=&quot; attribute.
[tt]
text[/tt] property is for whatever is between <option> and </option>

hope this helps!
=========================================================
if (!succeed) try();
-jeff
 
I must not be explaining this correctly..

EXPLAINATION::

Lets say I have a database.. I also have an HTML form to add/edit the database.. If a user already has data in the database then the form should already be filled out with the data on load. This means the text field values, radion button values, and of course pulldown menu's.

EXAMPLE::

If the database value of the pulldown menu for &quot;country&quot; and it is &quot;AK&quot; then the pulldown will have &quot;Arkansas&quot; already selected for the user.

If the database value of the pulldown menu for &quot;country&quot; and it is &quot;AL&quot; then the pulldown will have &quot;Alabama&quot; already selected for the user.

If there is no value in the database for &quot;country&quot; then the pulldown menu will not have any pre-selected values.

So basically what i am trying to do is create a javascript that can accomplish this. I am using a template system to fill out the javascript values and got it to work with radio buttons and text fields. I just can get it to work with pulldowns.


This is the form I am using:

<form name=theForm>
<select name=country>
<option value=&quot;&quot;>
<option value=&quot;AL&quot;>Alabama
<option value=&quot;AK&quot;>Alaska
<option value=&quot;AS&quot;>Am. Samoa
</select>

This is what currently works..

<script language=&quot;javascript&quot;>
<!--//
function SelectOptions() {
document.theForm.gender[<%TEMPLATE VALUE FOR GENDER%>].checked = true; // This selects the radio button
document.theForm.name.value = &quot;<%TEMPLATE VALUE FOR NAME%>&quot;; // This fills in the name field

}
//-->
</script>

Just need it to work with pulldowns...
 
I guess what you're talking about is creating <select> list using some values sent by server script. Is this what you want? If yes, here's an example:
[tt]
var newOpt = new Option();
newOpt.value = some_name;
newOpt.text = some_name;
var n = document.formName.selectName.length;
document.formName.selectName.options[n] = newOpt;
[/tt]
The script will add a new option at the end of already existing select list.
You can make it shorter:
[tt]var newOpt = new Option('name', 'value');[/tt]
I just don't remember exactly what comes first: name or value.
You can also use this in a loop to generate the whole list.
 
It sounds to me like this is what you want:
Code:
window.onload=selectCountry;
function selectCountry(){
  var selectedCountry='<%=recordsetObject.Fields(&quot;Country&quot;)%>';
  for(var i=0;i<document.formName.Country.length;i++){
    if(document.formName[i].value==selectedCountry){
      document.formName.selectedIndex=i;
      break;
    }
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top