Reputation: 4954
I've got an option-select dropdown autopopulating using data pulled from an oracle database using asp-classic. The option's id's are the employee id numbers. I'm doing a query to a database to get the employee id who was working during the shift. Once I get that ID, I want to set that option to be the currently selected option, but it's not working for me. Here's what I've got:
<select id="supervisor1" style="width:90%">
<option value="1234" id="1234">John1 Smith </option>
<option value="1235" id="1235">John2 Smith </option>
<option value="1236" id="1236">John3 Smith </option>
<option value="1237" id="1237">John4 Smith </option>
<option value="1345" id="1345">John5 Smith </option>
<option value="1346" id="1346">James Smith </option>
</select>
So that's an example of the select. Obviously names and id numbers have been changed, and there are about 50 names in the actual program.
Now, when the ASP code executes on pageload, the current supervisor is stored in an ASP variable called "foreman1".
<script>
var selectedIndex = '<%=foreman1%>'
var sup1 = document.getElementById('supervisor1');
//alert(selectedIndex);
sup1.options[sup1.options.selectedIndex].selected = true;
</script>
This stores the value of 'foreman1' in a javascript variable. It stores the supervisor selectbox in a variable as well. Then it is supposed to go through the options of supervisor1, find the one with the id of 'foreman1' and then make it selected, which I imagine would make it show up on the page as being the one that is selected. however, it isn't doing this. What am I doing wrong?
Upvotes: 0
Views: 1234
Reputation: 318322
If selectedIndex
is the value of the option you'd like to select, just set the selects value to the same value
<script>
var selectedIndex = '<%=foreman1%>'
var sup1 = document.getElementById('supervisor1');
sup1.value = selectedIndex;
</script>
and place the script tag before </body>
Upvotes: 1