views:

6809

answers:

3

I essentially have the same situation as the person in the following question:

Link: how to show/hide divs by select.(jquery)

Through extensive searching within Google I was able to come up with several different methods in which people claim their method works. I have yet to get any to work correctly yet. I don't yet know enough about jQuery to fully understand how to write this from scratch, thus I rely on really good examples for now.

What I've been trying to work with (based on examples I've found and tried) is this:

<script type="text/javascript">
    (document).ready(function() {
        ('.box').hide();<br/>
        ('#dropdown').change(function() {
        ('#divarea1')[ ($(this).val() == 'area1') ? 'hide' : 'show' ]()
        ('#divarea2')[ ($(this).val() == 'area2') ? 'hide' : 'show' ]()
        ('#divarea3')[ ($(this).val() == 'area3') ? 'hide' : 'show' ]()
        });
    });
</script>
<form>
    <select id="dropdown" name="dropdown">
        <option value="0">Choose</option>
        <option value="area1">DIV Area 1</option>
        <option value="area2">DIV Area 2</option>
        <option value="area3">DIV Area 3</option>
    </select>
</form>
<div id="divarea1" class="box">DIV Area 1</div>
<div id="divarea2" class="box">DIV Area 2</div>
<div id="divarea3" class="box">DIV Area 3</div>
  • Note: I am using brackets rather than the less-than and greater-than signs around html to display correctly in this message.

What I get when I test this:

On first load with nothing selected => No DIV is display. When I select DIV Area 1 => DIV Area 2 and 3 are displayed. When I select DIV Area 2 => DIV Area 1 and 3 are displayed. When I select DIV Area 3 => DIV Area 1 and 2 are displayed.

My brain is fried for the day. What can I do to fix this?

+1  A: 

Swap show/hide so that it looks like this:

$('#divarea1')[ ($(this).val() == 'area1') ? 'show' : 'hide' ]()
T. Stone
I apologize for not catching this when I originally posted... In my working code, the dollar signs are present. They must have been stripped away when I posted my code.
Thomas Grant
+1  A: 

This code is a little more succinct:

$(document).ready
(
  function()
  {
    $("div.box").hide();
    $("#dropdown").change
    (
      function()
      {
        var selectedValue = $(this).val();
        if(selectedValue !== "0")
        {
          $("div.box").show();
          $("#div" + selectedValue).hide();
        }   
      }   
    );
  }
};

Essentially, if there is a value selected (i.e., the option is not set to "Choose"), then all div.box elements are shown. The DIV matching the selected option is then hidden. This should happen quickly enough so that the flash is not noticeable.

David Andres
You may have misunderstood. I don't want ANY of the DIV elements to be shown initially at all. I only want ONE to be displayed once a selection has been made.
Thomas Grant
@Thomas Grant: Doesn't $("div.box").hide() the elements on page load?
David Andres
A: 

I'd do this:

<script type="text/javascript">
$(document).ready(function(){
 $('.box').hide();
  $('#dropdown').change(function() {
    $('.box').hide();
    $('#div' + $(this).val()).show();
 });
});
</script>
<form>
 <select id="dropdown" name="dropdown">
  <option value="0">Choose</option>
  <option value="area1">DIV Area 1</option>
  <option value="area2">DIV Area 2</option>
  <option value="area3">DIV Area 3</option>
 </select>
</form>
<div id="divarea1" class="box">DIV Area 1</div>
<div id="divarea2" class="box">DIV Area 2</div>
<div id="divarea3" class="box">DIV Area 3</div>
fudgey
After reading your suggested code, I realized that in my three lines such as:('#divarea1')[ ($(this).val() == 'area1') ? 'hide' : 'show' ]();I initially forgot the trailing semi-colon. This may have caused some of the problems I was experiencing. I however tried out your code and it's performing exactly what I was looking for.Thank you!
Thomas Grant
If you have your answer, could you please close out the question by checking the mark next to the number, thank you :)
fudgey
how would this work if you wanted to, on page load, show the div corresponding to the dropdown's selected item? that is, i'm setting the selected item server-side, then when the page is rendered, show the appropriate div (as oppposed to all or none of them). Thanks!
Mark Richman
@Mark Richman: If you add a selected attribute to the option server side, then just add this below the `$('.box').hide();` line (inside the document ready function).... `$('#div' + $('#dropdown').val()).show();`
fudgey