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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatically choosing old value in Select box 1

Status
Not open for further replies.

Katerine

Programmer
Joined
Mar 9, 2001
Messages
234
Location
US
Hi,
I'm kind of new to JavaScript - more of a VB programmer. So this may be a simple fix; I don't know.

I've got a form (pharmreg) that has two select boxes (memberof and conferenceoption). The options in conferenceoption depend on the value selected in memberof, because depending on what you are a member of, the prices change.

So, basically, there are three possible options for "memberof": "WNA", "WAPNAP", and "Non." And there are always three options for "conferenceoption":
"Two Days (Friday & Saturday)($someprice)"
"Friday Only ($someprice)"
"Saturday Only ($someprice)"
The value of "someprice" changes depending on what's selected in memberof.

So, I've got code that sets the options in conferenceoption depending on the value in memberof, using an array. That code works.

What doesn't work is my efforts to keep the value in conferenceoption the same. That is, if a person selects "Friday Only" in conferenceoption, then selects "WNA" in memberoption, I want the code to change the options in conferenceoption to show the WNA prices, and then re-select "Friday Only." But it doesn't do that: it changes the options, then sets the value back to "Two Days," which is the first option in the list.

Here's the code that's not working. I have tried several variations; this particular variation is the one recommended for automatically setting values, but it's giving me a runtime error ("'options[...]' is null or not an object"):

[tt]
Code:
function SelectControlOption(selectCtrl,optionindex) {
 selectCtrl.options[optionindex].selected='true';
}

function SetPriceDisplays() {
 var ctlconfoption=document.forms.pharmreg.conferenceoption;
 var currentconfindex=ctlconfoption.selectedindex;
 switch(document.forms.pharmreg.memberof.value) {
  case "WNA": { fillSelectFromArray(document.forms.pharmreg.conferenceoption, confvalue[0]); break; }
  case "WAPNAP": { fillSelectFromArray(document.forms.pharmreg.conferenceoption, confvalue[1]); break; }
  case "Non": { fillSelectFromArray(document.forms.pharmreg.conferenceoption, confvalue[2]); break; }
 }
 
 SelectControlOption(ctlconfoption,currentconfindex);
}

// here's a bunch of irrelevant form code. following is html.

	 <tr>
	  <td width=&quot;75&quot;></td>
	  <td align=&quot;left&quot; width=&quot;100&quot;><font class=&quot;smalltext&quot;><b>:</b></font></td>
	  <td align=&quot;left&quot; width=&quot;450&quot;>
		 <select size=&quot;1&quot; name=&quot;memberof&quot; onchange=SetPriceDisplays()>
		  <option value=&quot;WNA&quot;>WNA Member</option>
		  <option value=&quot;WAPNAP&quot;>WAPNAP Member</option>
		  <option value=&quot;Non&quot; selected=&quot;selected&quot;>Non-Member</option>
		 </select>
		</td>
	 </tr>
	 <tr>
	  <td width=&quot;75&quot;></td>
	  <td align=&quot;left&quot; width=&quot;100&quot;><font class=&quot;smalltext&quot;><b>:</b></font></td>
	  <td align=&quot;left&quot; width=&quot;450&quot;>
		 <select size=&quot;1&quot; name=&quot;conferenceoption&quot;>
		  <option value=&quot;Two Days&quot;>                                 </option>
		  <option value=&quot;Friday&quot;>       </option>
		  <option value=&quot;Saturday&quot;>       </option>
		 </select>
		</td>
	 </tr>
[/tt]
Please help. Thanks in advance.

Katie
 
Well.... you aren't showing us any of your code that is actually doing any of the work, so I can't incorporate the answer into your code. However, this should be an easy fix so it shouldn't be too complicated integrate the solution into your code.

Change this line:
var currentconfindex=ctlconfoption.selectedindex;

To this:
var currentconfvalue=ctlconfoption.selectedindex.value;

Then after your drop down is built, set it equal to currentconfvalue:
pharmreg.conferenceoption.value = currentconfvalue;

This should search thru your pulldown and set the active member in the pulldown to whatever the previous value was. If the previous value does not exist, then it will default to the first option in the drop down



-kaht

banghead.gif
 
Try var currentconfindex=ctlconfoption.selectedIndex; in place of var currentconfindex=ctlconfoption.selectedindex;

Think it should be a I in place if i, other than that I can see nothing wrong :(



----------
I'm willing to trade custom scripts for... [see profile]
 
Thank you both very much for your help. I implemented both your suggestions, and what now happens is a) I no longer get an error message, and b) after changing the value of memberof, it now resets the value of conferenceoption to null. Which isn't quite what I wanted, but it's actually considerably better than resetting it to the first item in the list, because it's a much more visible change. I think I'll just keep it like this - this is good enough.

Thanks again!

Katie
 
While I'm certainly not the best programmer on this board, I didn't think you wanted &quot;selectedIndex.value&quot;

I think selectedIndex returns an integer. selectedIndex.value would return the value of the element which wouldn't have the desired effect if the rest of the script is expecting an integer.

----------
I'm willing to trade custom scripts for... [see profile]
 
You're right stormbind, I was trying to recreate from memory the 1 time I ever used selectedIndex. I looked up my oldcode and this would be the syntax translated to your problem:
Code:
var currentconfvalue=ctlconfoption.options(ctlconfoption.selectedIndex).value;
p.s. that's all one line, wordwrap is making it look like 2 lines on my screen when I hit preview

-kaht

banghead.gif
 
Thanks again for the help - it works!

FYI, the main original issues appear to have been the following:
1. I misread the web examples and used brackets instead of parens in referring to the following:
ctlconfoption.options(ctlconfoption.selectedIndex).value
2. I referred to selectedindex instead of selectedIndex.

Thanks :-)

Katie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top