I need to set an option from a select box to be selected based upon a value I am pulling from a database.
I cannot alter the code of the select box in this case, so I must use javascript to change it after the select box has loaded. There are several pages, some with several select boxes, that need this function. I would like one javascript function that will take take two variables (the form element and a value) and then set the correct option to be selected.
Here is an example of the code format for these select boxes. I can add id or value attributes to the options but this would be a very time consuming process as there are a large number that would need to be edited.
[HTML]
<select name="userans0" id="userans0">
<option></option>
<option>CR - Copy Recorded</option>
<option>CU - Copy Unrecorded</option>
<option>OU - Original unrecorded</option>
<option>OR - Original recorded</option>
<option>MI - Missing</option>
</select>
[/HTML]
Here is the javascript function I am trying to use, this is in the document header.
In this case the variables are the following:
formElement = document.testfo rm.userans0
val = CU - Copy Unrecorded
And the code to call the function.
[HTML]
<script type="text/javascript" language="JavaS cript">
selectValueA(fo rmElement, val);
</script>
[/HTML]
I can't seem to get this to work.
I cannot alter the code of the select box in this case, so I must use javascript to change it after the select box has loaded. There are several pages, some with several select boxes, that need this function. I would like one javascript function that will take take two variables (the form element and a value) and then set the correct option to be selected.
Here is an example of the code format for these select boxes. I can add id or value attributes to the options but this would be a very time consuming process as there are a large number that would need to be edited.
[HTML]
<select name="userans0" id="userans0">
<option></option>
<option>CR - Copy Recorded</option>
<option>CU - Copy Unrecorded</option>
<option>OU - Original unrecorded</option>
<option>OR - Original recorded</option>
<option>MI - Missing</option>
</select>
[/HTML]
Here is the javascript function I am trying to use, this is in the document header.
Code:
function selectValue(formElement, val) { for(i=0;i<formElement.length;i++) { if(formElement.options[i].value==val) { formElement.selectedIndex=i } } }
formElement = document.testfo rm.userans0
val = CU - Copy Unrecorded
And the code to call the function.
[HTML]
<script type="text/javascript" language="JavaS cript">
selectValueA(fo rmElement, val);
</script>
[/HTML]
I can't seem to get this to work.
Comment