I copied and pasted code a long time ago from someone providing it for free.
This code is for use with a form.
When you choose an *particular option in a selection box, another additional option appears (is unhid).
Works great. But, I wanted to adapt it because I need TWO hidden selection boxes on my page:
1) When a certain state is selected, THEN the selection box for cities under that state is unhid.
AND
2) when a certian category is chosen THEN a selection box for sub-categories is unhid.
The problem is I ALMOST got it to work, but I don't know javascript well enough to figure out why it is not working perfectly....
Both state and categories DO make their respective hidden elements appear BUT they also make the other un-hidden one hide again, so they are effecting one another...
This is the part of the code pasted into the header
Code:
<script type="text/javascript"><!--
if(document.all && !document.getElementById) { //IE4 support
document.getElementById = function(id) {
return document.all[id];
}
}
/*
This works in Firefox, Netscape 6+, IE4+/Win, Opera 7+, Konqueror 3, Safari,
IE5/Mac, and iCab 3.
*/
function customOption(selected) { //selected is the selected option
if(!document.getElementById) return;
// selected's value property is retrieved and converted to lower case to make
// comparing it to another string simpler
var val = selected.value.toLowerCase();
// gets the object reference for the element
var el = document.getElementById('other1label');
var e2 = document.getElementById('other2label');
// if val is set to 'customoption' show the textbox
if(val == 'customoption') {
el.style.display='block';
}
else { // otherwise hide it or keep it hidden.
el.style.display='none';
}
if(val == 'customoption2') {
e2.style.display='block';
}
else { // otherwise hide it or keep it hidden.
e2.style.display='none';
}
}
function dss_addLoadEvent(fn) {
if(typeof(fn)!="function")return;
var tempFunc=window.onload;
window.onload=function() {
if(typeof(tempFunc)=="function")tempFunc();
fn();
}
}
dss_addLoadEvent(function() {
// we find the form element and the select element and attach the event
// handlers to them
var f = document.form1;
// this next line of code is used to prevent this demo from being submitted
// remove it or change it as needed
// f.onsubmit = function(){return false;}
var sel = f.select1;
var sel2 = f.select2;
sel.onchange=function(){customOption(this.options[this.selectedIndex])}
sel2.onchange=function(){customOption(this.options[this.selectedIndex])}
// we call the function when the page loads to hide #other1label initially
sel.onchange();
sel2.onchange();
});
// -->
</script>
and this is the part of the code used in the form itself
Code:
<form action="'.$_SERVER['PHP_SELF'].'" name="form1" method="post">
<div class="cont">
<div class="label">
<label for="select1">
Assigned State / Location 1: <select name="select1" id="select1"><option value="1">state 1</option><option value="2">state 2</option>
<option value="customoption">prompt city popup test...</option></select>
</select>
</label>
</div>
<div class="label" id="other1label">
<label for="other1">Assigned City: <select name="cityx"><option value="1">city one</option></select>
</label>
</div>
</div>
<div class="cont">
<div class="label2">
<label for="select2">
Assigned Category: <select name="select2" id="select2">
<option>Please make a selection</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="black">Black</option>
<option value="customoption2">Other...</option>
</select>
</label>
</div>
<div class="label2" id="other2label">
<label for="other2">Sub-category: <select name="subcatx"><option value="1">This subcat</option><option value="1">That subcat</option><option value="1">Other subcat</option></select>
</label>
</div>
</div>
</form>
THANK YOU!!!