DBookatay Posted March 7, 2007 Share Posted March 7, 2007 I dont know if what these are really called, but in php I think their called "case statements." nogrey & fenway gave me a great code for dropdown hidden layers: java code: function show_hide(val) { if ((val == "0") || (val == "1")) document.getElementById('ap_preWork').style.display = ""; else document.getElementById('ap_preWork').style.display = "none"; } html code: <select name="ap_wrk10" onchange="show_hide(this.value);"> What I'm trying to do is make it so that the code can be used on a few different layers, "previous work", "previous address", ect. I wrote this: function show_hide(val) { if ((val == "") || (val== "0") || (val == "1")) { switch (val) { case 'AA': document.all('apAdd').style.display = ""; break; case 'AE': document.all('apWrk').style.display = ""; break; } } else { switch (val) { case 'AA': document.all('apAdd').style.display = "none"; break; case 'AE': document.all('apWrk').style.display = "none"; break; } } The html: <select name="ap_add5" onchange="show_hide('AA',document.forms[0].apAdd.value);"> This doesnt work, so obviously something is wrong. Problem is, I have NO IDEA what I'm doing, or how to go about getting this to work... Is my javascript code wrong, is the html code wrong, or is all of it just junk? Any ideas or advise would really help. Thank you... Quote Link to comment Share on other sites More sharing options...
DBookatay Posted March 7, 2007 Author Share Posted March 7, 2007 Anyone? Any ideas? Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted March 7, 2007 Share Posted March 7, 2007 Anyone? Any ideas? In your function declaration, you should probably tell it to expect two arguments instead of just one. Something like: function show_hide(filter, val) { if ((val == "") || (val== "0") || (val == "1")) { switch (filter) { case 'AA': document.all('apAdd').style.display = ""; break; case 'AE': document.all('apWrk').style.display = ""; break; } } else { switch (filter) { case 'AA': document.all('apAdd').style.display = "none"; break; case 'AE': document.all('apWrk').style.display = "none"; break; } } The reason is because you're passing two arguments to the function (something like 'AA' and the actual value of the select), but you're trying to use one argument to do both jobs in your function's code. In other words, your select value, judging by what I can see in your code, can't be both a number not equal to 0 or 1 and one of your strings (i.e. 'AA') at the same time. Hope this helps. Quote Link to comment Share on other sites More sharing options...
fenway Posted March 7, 2007 Share Posted March 7, 2007 Also, I'd only have a single switch, and deal with the outer if() condition as a inner one. Quote Link to comment Share on other sites More sharing options...
mainewoods Posted March 8, 2007 Share Posted March 8, 2007 If you pass the id of the item as one of the parameters, you can simplify it further: function show_hide(theID, val) { if ((val == "") || (val== "0") || (val == "1")) { document.all(theID).style.display = ""; } else { document.all(theID).style.display = "none"; } } --You should change it to 'getElementById' instead of 'document.all' - more cross browser compatable Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.