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

Set Select Box Selected by Value? 1

Status
Not open for further replies.

jennuhw

MIS
Apr 18, 2001
426
US
I have tried many combinations, but none of them are working, and surprisingly, I don't end up getting an error. The select box ends up having nothing selected. I need to set the selected option of a select box by the value since I don't know the index. Is this possible? Here is my code:

Code:
<script language="javascript">
	function fill_profiles()
	{ // first, remove all what is in the listbox

		document.form1.osprofile.disabled = false
		my_listbox=document.form1.osprofile
		//chosen=document.form1.vendor.value

		while (my_listbox.options.length!=0) 
			my_listbox.options.remove(0);

		var oOption = document.createElement("OPTION");
		oOption.text=''
		oOption.value='%';
		my_listbox.options.add(oOption)

		// now, fill it !
		//alert(document.form1.vendor.value)
		if (document.form1.vendor.value == '0') {
			for (var m=0; m<esparray.length; m++) 
				// if you conditionally fill the list, put the condition here (for example : if (my_array[m][1]>"whatever"))
		   {
				if (esparray[m] != '') {
					var oOption = document.createElement("OPTION");
					oOption.text=esparray[m];
					oOption.value=esparray[m];
					my_listbox.options.add(oOption)
				}
		   }
		}

		if (document.form1.vendor.value == '1') {
			for (var m=0; m<crarray.length; m++) 
				// if you conditionally fill the list, put the condition here (for example : if (my_array[m][1]>"whatever"))
		   {
				if (crarray[m] != '') {
					var oOption = document.createElement("OPTION");
					oOption.text=crarray[m];
					oOption.value=crarray[m];
					my_listbox.options.add(oOption)
				}
		   }
		}	 
		
		if (document.form1.vendor.value == '2') {
			for (var m=0; m<natarray.length; m++) 
				// if you conditionally fill the list, put the condition here (for example : if (my_array[m][1]>"whatever"))
		   {
				if (natarray[m] != '') {
					var oOption = document.createElement("OPTION");
					oOption.text=natarray[m];
					oOption.value=natarray[m];
					my_listbox.options.add(oOption)
				}
		   }
		}

		if (document.form1.vendor.value == '3') {
			for (var m=0; m<nokarray.length; m++) 
				// if you conditionally fill the list, put the condition here (for example : if (my_array[m][1]>"whatever"))
		   {
				if (nokarray[m] != '') {
					var oOption = document.createElement("OPTION");
					oOption.text=nokarray[m];
					oOption.value=nokarray[m];
					my_listbox.options.add(oOption)
				}
		   }
		}

		if (document.form1.vendor.value == '4') {
			for (var m=0; m<transarray.length; m++) 
				// if you conditionally fill the list, put the condition here (for example : if (my_array[m][1]>"whatever"))
		   {
				if (transarray[m] != '') {
					var oOption = document.createElement("OPTION");
					oOption.text=transarray[m];
					oOption.value=transarray[m];
					my_listbox.options.add(oOption)
				}
		   }
		}

		if (document.form1.vendor.value == '5') {
			for (var m=0; m<nuarray.length; m++) 
				// if you conditionally fill the list, put the condition here (for example : if (my_array[m][1]>"whatever"))
		   {
				if (nuarray[m] != '') {
					var oOption = document.createElement("OPTION");
					oOption.text=nuarray[m];
					oOption.value=nuarray[m];
					my_listbox.options.add(oOption)
				}
		   }
		}
//
//
//
//
//
//Here is the line.  I have done my_listbox.selectedValue and many other combos
		my_listbox.selectedIndex = '<%=request.form("osprofile")%>';
		return true; }
</script>


<form method=post name="form1" id="form1">
<p align=center><table><tr>

	<!-- ****************************oil seals profiles****************************** -->
	<td align=right>Vendor:  </td>
	<td><select name="vendor" id="vendor" onchange="fill_profiles()">
		<option value="%" <%if request.form("vendor")="%" then%>selected<%end if%>></option>
		<option value="0" <%if request.form("vendor")="0" then%>selected<%end if%>>ESP</option>
		<option value="1" <%if request.form("vendor")="1" then%>selected<%end if%>>Chicago Rawhide</option>
		<option value="2" <%if request.form("vendor")="2" then%>selected<%end if%>>National</option>
		<option value="3" <%if request.form("vendor")="3" then%>selected<%end if%>>Fruedenberg - NOK</option>
		<option value="4" <%if request.form("vendor")="4" then%>selected<%end if%>>Transcom</option>
		<option value="5" <%if request.form("vendor")="5" then%>selected<%end if%>>Nu-Seals</option>
	</select></td>

	<td align=right>Profile:  </td><td><select name="osprofile" id="osprofile" <%if request.form("osprofile") = "" then%> disabled <%end if%>></select></td>
 
I've not tested this, but I think you can replace your whole script section with this:

Code:
<script type="text/javascript">
<!--
	function fill_profiles() {
		var frm = document.forms['form1'].elements;
		my_listbox = frm['osprofile'];
		my_listbox.disabled = false;
		my_listbox.options.length = 0;

		var oOption = document.createElement('option');
		oOption.text = '';
		oOption.value = '%';
		my_listbox.options.add(oOption);

		var sourceList = null;
		switch (frm['vendor'].value) {
			case 0: sourceList = esparray; break;
			case 1: sourceList = crarray; break;
			case 2: sourceList = natarray; break;
			case 3: sourceList = nokarray; break;
			case 4: sourceList = transarray; break;
			case 5: sourceList = nuarray; break;
		}

		for (var loop=0; loop<sourceList.length; loop++) {
			if (sourceList[loop] != '') {
				var oOption = document.createElement('option');
				oOption.text = sourceList[loop];
				oOption.value = sourceList[loop];
				if (sourceList[loop] == '<%=request.form("osprofile")%>') my_listbox.selectedIndex = loop + 1;
				// The +1 is to take into account the default option with a value of "%" that was added outside this loop
				my_listbox.options.add(oOption);
			}
		}
		
		return true;
	}
//-->
</script>

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hmm.. actually, you should add the option before setting the selectedIndex... so move this line:

Code:
my_listbox.options.add(oOption);

to before this line:

Code:
if (sourceList[loop] == '<%=request.form("osprofile")%>') my_listbox.selectedIndex = loop + 1;

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Ah yes - another flaw (told you I hadn't tested it ;-)).... if the entry in the array is blank, the index will be out... so this part:

Code:
my_listbox.selectedIndex = loop + 1;

should really be this, for safety's sake:

Code:
my_listbox.selectedIndex = my_listbox.options.length - 1;

as the option you wish to select will always be the last option added.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thank you so much for your help! Works fine with some tweaking!
 
I felt that the 5 "if" statements could all do with compacting... originally I was going to put a function call in them, and then I realised I could just assign the relevant source array to a temporary variable.

Good to see you managed to integrate it OK.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top