views:

116

answers:

9

Hi guys, my Javascript switch case isn't working for some reason, and I can't figure it out, I am trying to display a certain input only of a certain option is chosen,

function showHideSchools(obj){ 

    var curSel=obj.options[obj.selectedIndex].value 


    switch(curSel)
    {
    case '0-2':
    document.getElementById('schools').style.display="none" 
      break;        
    case '3-5':
    document.getElementById('schools').style.display="block" 
      break;
    case '6-8':
    document.getElementById('schools').style.display="block" 
      break;
    case '9-11':
    document.getElementById('schools').style.display="block" 
      break;
    case '12-14':
    document.getElementById('schools').style.display="block" 
      break;
    case '15-16':
    document.getElementById('schools').style.display="block" 
      break;
    case '17-18':
    document.getElementById('schools').style.display="block" 
      break;
    case '19 and over':
    document.getElementById('schools').style.display="block" 
      break;
    default:
    document.getElementById('schools').style.display="none"
    }
}  

Here is the HTML:

<p>
  <label for="childrenAges">Ages of children still living at home: </label>
  <select name="childrenAges" id="childrenAges"  onchange="showHideSchools(this);">
    <option>Select one</option>
    <option value="0-2">0-2</option>
    <option value="3-5">3-5</option>
    <option value="6-8">6-8</option>
    <option value="9-11">9-11</option>
    <option value="12-14">12-14</option>
    <option value="15-16">15-16</option>
    <option value="17-18">17-18</option>
    <option value="19 and over">19 and over</option>
  </select>
</p>
<div id="schools" style="display:none">
    <p>
        <label for="schoolName">What school/s do they attend: </label>
        <input type="text" name="schoolName" />
    </p>
</div>
A: 

The case values are wrong, they have to match the value in the <option> tags, not the displayed text.

So they would be

case '1': 
    document.getElementById('schools').style.display="block"  
      break;         
    case '2': 
    document.getElementById('schools').style.display="block"  
      break;

etc

JoseK
A: 

From your code, it looks like you'd be better off switching the criteria, so that you hide when the value is "" and show the box in all other cases - that looks like what you're trying to do...?

Other than that, I'm not 100% sure what your problem is, but if the problem is that it's not working for the first three items, then the reason is that their values will be 1, 2 and 3, not 0-2 etc, that you're testing for.

David Hedlund
Well, it seems to be working with a simple if statement, if(curSel=="yes") {...etc}else{...etc} but when I try to implement the switch it just WONT work, even though I can't see anything wrong with it.
+1  A: 

The value of the options 0-2, 3-5 and 6-8 are 1, 2 and 3 respectively. You have them as 0-2, 3-5 and 6-8 in your javascript.

Chris
Whoops, sorry, that was a typo, that wasn't what I had in my file.
A: 

You are not specifying the option values rather their representation in switch, for example:

case '0-2':

There is nothing like that in the select's option values.

Sarfraz
+6  A: 

You don't need a switch case :

if(obj.options[obj.selectedIndex].value != "Select one" && obj.options[obj.selectedIndex].value != "0-2"){
    document.getElementById('schools').style.display="block";
}else{
    document.getElementById('schools').style.display="none";
}
Kaaviar
Display is none on "0-2" as well.
Josh Stodola
He may intent to do different things depending on what option was selected at some point. At least, that's the only reason I can fathom for all those redundant case blocks.
no
@Josh : you're right I didn't see that one. You can add a OR statement@no : couldn't guess that ;)
Kaaviar
Yup, what no said. This is just the preliminary "Get it to work" phase, I will try and clean up the code and make it more efficient once I have gotten it to work as intended.
You shouldn't access the drop down with `document.getElementById("childrenAges")` (especially not twice). Providing a reference as a parameter as kielie did is better.
RoToRa
OMG ! Playing to much with jQuery and you'll forget any simple JS !
Kaaviar
+1  A: 

0-2 3-5 6-8

case '0-2': case '3-5':

Your values and case params do not correspond. When you select '0-2', .value is "1"

Daniel Kluev
A: 

case '1': ... case '2': ... case '3': ... case '9-11': ... case '12-14': ...

I.e. you should check the content of option's value attribute, not text inside tags

Dennis Crane
+3  A: 

As others said, your case tests didn't match the value of the first three options.

There's no reason to repeat that line document.getElementById('schools').style.display="block"; over and over, just let all the conditions that lead to that outcome fall through to a single line with that instruction.

function showHideSchools (obj) { 

  var curSel=obj.options[obj.selectedIndex].value;

  switch (curSel) {
    case '2':
    case '3':
    case '9-11':
    case '12-14':
    case '15-16':
    case '17-18':
    case '19 and over':
      document.getElementById('schools').style.display="block"; 
      break;
    case '1':
    default:
      document.getElementById('schools').style.display="none";
  }

}  
no
A: 

Just set up a jsfildde for it: http://jsfiddle.net/nHg9t/ . This seems to work fine as far as I can see... Can you take a look at this and say if it is doing the right thing and if not what is wrong and if so how your tests differ...

Chris