J
JCK
I have a drop down select form field and when a user makes a selection
it triggers a function that changes the style of a hidden div that
contains an id that matches the option value of the selected option.
So for example if one selected Optimize below in the drop down menu a
div with id Optimize would have its style changed from the default
none to inline.
function revealFormField(fieldname,value) {
document.getElementById(value).style.display = 'inline';
}
<select name="table" id="table" onchange="revealFormField
('table',this.options[this.selectedIndex].value);">
<option value="" selected="selected">Select One</option>
<option value="Check">Check</option>
<option value="Optimize">Optimize</option>
<option value="Repair">Repair</option>
</select>
This works but if someone subsequently changes the Select drop down to
something else the div from the previously selected option still shows
so I would like to add some code to the function to make sure all divs
containing an id that match any of the option values to be set to
hidden before changing the the one selected to inline. Below is my
attempt to modify the function to do so but it fails with "Object
required". I suspect it is because the option values are strings and
not objects but I don't know how to convert them or what else is wrong
with my function.
function revealFormField(fieldname,value) {
for(var i = 1; i <= document.getElementById(fieldname).length; i++) {
var option_name = document.getElementById(fieldname).options.value;
document.getElementById(option_name).style.display = 'none'; // LINE
THAT HAS ERROR
}
document.getElementById(value).style.display = 'inline';
}
it triggers a function that changes the style of a hidden div that
contains an id that matches the option value of the selected option.
So for example if one selected Optimize below in the drop down menu a
div with id Optimize would have its style changed from the default
none to inline.
function revealFormField(fieldname,value) {
document.getElementById(value).style.display = 'inline';
}
<select name="table" id="table" onchange="revealFormField
('table',this.options[this.selectedIndex].value);">
<option value="" selected="selected">Select One</option>
<option value="Check">Check</option>
<option value="Optimize">Optimize</option>
<option value="Repair">Repair</option>
</select>
This works but if someone subsequently changes the Select drop down to
something else the div from the previously selected option still shows
so I would like to add some code to the function to make sure all divs
containing an id that match any of the option values to be set to
hidden before changing the the one selected to inline. Below is my
attempt to modify the function to do so but it fails with "Object
required". I suspect it is because the option values are strings and
not objects but I don't know how to convert them or what else is wrong
with my function.
function revealFormField(fieldname,value) {
for(var i = 1; i <= document.getElementById(fieldname).length; i++) {
var option_name = document.getElementById(fieldname).options.value;
document.getElementById(option_name).style.display = 'none'; // LINE
THAT HAS ERROR
}
document.getElementById(value).style.display = 'inline';
}