Jump to content

Stumped on a "switch case" statement.


DBookatay

Recommended Posts

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...

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.