views:

258

answers:

2

Hello,

This question refers to the question http://stackoverflow.com/questions/835259/jquery-show-hide-fields-depening-on-select-value

<select id="viewSelector">
   <option value="0">-- Select a View --</option>       
   <option value="view1">view1</option>
   <option value="view2">view2</option>
   <option value="view3">view3</option>
</select>

<div id="view1">
  <!-- content --> 
</div>
<div id="view2a">
  <!-- content --> 
</div>
<div id="view2b">
  <!-- content --> 
</div>
<div id="view3">
  <!-- content --> 
</div>



$(document).ready(function() {
  $.viewMap = {
    '0' : $([]),
    'view1' : $('#view1'),
    'view2' : $('#view2a, #view2b'),
    'view3' : $('#view3')
  };

  $('#viewSelector').change(function() {
    // hide all
    $.each($.viewMap, function() { this.hide(); });
    // show current
    $.viewMap[$(this).val()].show();
  });
});

When I select the 2nd item in the menu it shows the corresponding field.

The exception to this is when the on page load the select menu already has the 2nd menu item selected, the field does not show.

As you may be able to tell, I am new to jquery and could definetly use some help with adjusting this code so that the selected item's field is shown on page load.

Thanks,

Tim

A: 

Move the code into a function and call it onload as well -

 $(document).ready(function() {

  function toggleDivs(val) {
        $.each($.viewMap, function() { this.hide(); });
        $.viewMap[val].show();
  }

  toggleDivs($('#viewSelector').val());

  $.viewMap = {
        '0' : $([]),
    'view1' : $('#view1'),
    'view2' : $('#view2a, #view2b'),
    'view3' : $('#view3')
  };

  $('#viewSelector').change(function() {
    toggleDivs($(this).val());
  });
});

Of course, you probably wouldn't need $.viewMap if you just stored the selector data in the HTML of the <option>.. data-selectors="#view2a, #view2b"

Jeriko
Thanks for your quick reply. Unfortunately I am not having any joy. Nothing is loading any more and I don't know how to debug it.
Tim
+2  A: 

The easiest thing to do in this case is just trigger the change handler you already have/need, like this:

$('#viewSelector').change(function() {
  $.each($.viewMap, function() { this.hide(); });
  $.viewMap[$(this).val()].show();
}).change(); //only change to your code!

This just triggers the change event on the same selector right after you bound it, so it occurs on document.ready (where you're binding) as well as when it changes. .change() with no arguments is equivalent to .trigger('change').

Nick Craver
+1 for simplicity
Jeriko
Thats exactly what I was after! So thankyou. The Jquery in the autocomplete field that I just revealed has stopped working though. if you have any hints on why that may be, please help!
Tim
All other Jquery scripts have stopped working on this page since adding .change(); any clues?
Tim
Ok, fixed it. I just reordered the scripts so that the .change(); is last and it's all working. Appreciate your help dude!
Tim