views:

39

answers:

3

I have a selectbox with three options. When a user selects one of the three options, I want a specific div to appear below it. I am trying to write the code that dictates which specific box is to appear when each of the three options is selected. So far, I have only worked on the code that pertains to the first option. However, whenever the user selects any of the three options from the selectbox, the function for the first option is triggered and the div is displayed.

My question is two part: 1) How do I write a conditional function that specifically targets the selected option

2) What is the best way to accomplish what I have described above; How do I efficiently go about defining three different functions for three different options in a select box?

Here is the function I was working on for the first option:

$(document).ready(function(){
var subTableDiv = $("div.subTableDiv");
var subTableDiv1 = $("div.subTableDiv1");
var subTableDiv2 = $("div.subTableDiv2");
subTableDiv.hide();
subTableDiv1.hide();
subTableDiv2.hide();

var selectmenu=document.getElementById("customfields-s-18-s");
selectmenu.onchange=function(){ //run some code when "onchange" event fires
 var chosenoption=this.options[this.selectedIndex].value //this refers to "selectmenu"
 if (chosenoption.value ="Co-Op"){
     subTableDiv1.slideDown("medium");
     }
 }
});

Html:

<tr>
<div>
<select name="customfields-s-18-s" class="dropdown" id="customfields-s-18-s" >
<option value="Condominium"> Condominium</option>
<option value="Co-Op"> Co-Op</option>
<option value="Condop"> Condop</option>
</select>
</div>
</tr>
<tr class="subTable">
<td colspan="2">
<div style="background-color: #EEEEEE; border: 1px solid #CCCCCC; padding: 10px;" id="Condominium" class="subTableDiv">Hi There! This is the first Box</div>
</td>
</tr>
<tr class="subTable">
<td colspan="2">
<div style="background-color: #EEEEEE; border: 1px solid #CCCCCC; padding: 10px;" id="Co-Op" class="subTableDiv1">Hi There! This is the Second Box</div>
</td>
</tr>
<tr class="subTable">
<td colspan="2">
<div style="background-color: #EEEEEE; border: 1px solid #CCCCCC; padding: 10px;" id="Condop" class="subTableDiv2">Hi There! This is the Third Box.</div>
</td>
</tr>
+1  A: 

I think you can get this using the position of the item in the list and the table, as long as those relative positions are the same. Change the class on the DIVs so they are all subTableDiv.

$(function() {
    $('#customfields-s-18-s').change( function() {
         var selected = $(this).find('option:selected');
         var position = $(this).find('option').index(selected);
         // hide all then show the nth one
         $('.subTableDiv').hide().eq(position).show();
    });
});
tvanfosson
Cool, this shows the correct div when each option is selected but is there a way I can define which option/div is displayed by default? Right now all three divs are displayed but I would like to set it up so that the first option/div are the only ones being displayed.
Thomas
Set style `display: none` on the other divs by default. It might get a little trickier because you have them wrapped in a table. I'm not sure how the table rows will display when the divs are hidden since I don't know what the rest of your CSS is like. You might need/want to adjust it to show/hide the rows instead.
tvanfosson
+1  A: 

You can use selectmenu.value (or $(selectmenu).val()) to get the value of the selected option, and you can match the functions to the values using an object. Example:

$(function() {
  var call_table = {
    'Condominium': function() {alert('One!');},
    'Co-Op': function() {alert('Two!');},
    'Condop': function() {alert('Three!');}
  };

  $('#customfields-s-18-s').change(function() {
    call_table[this.value]();
  });
});

Of course, you don't have to define the functions inline. I just did it for concision here. You could define them anywhere and reference them by name instead.

Max Shawabkeh
Im not sure how this is suppossed to work - what does selectmenu.value return? The value of the selected option? I plugged this code in but I can get it to work - do I need to put it together with some kind of onChange trigger?
Thomas
The jQuery `change` function is the onChange handler. `this.value` should return the value of the select box. The problem was that I missed the hash for the id selector; fixed now.
Max Shawabkeh
So I can just plugin the function I want in place of:function() {alert('One!');}
Thomas
@Thomas: Yes, that is correct.
Max Shawabkeh
Awesome I got it! - Thank you
Thomas
+1  A: 

It looks like the select option values are the same as the IDs for the divs. You could use that to define a function that basically shows the div that has the same id as the value of the selected option. Also change the class on each div to subtableDiv.

$("#customfields-s-18-s").change(function() {
    // hide all divs
    $('.subtableDiv').hide();
    // show matching div
    var value = $(this).val();
    $('#' + value).show();
}
Anurag