views:

10844

answers:

7

Hello, I am trying to show and hide a few form fields dependent on the value of one of my select fields, I am looking to use arrays to to hold what should be show and what should not be show for each select value, to save me from a massive switch statement, but cannot figure out to do it.

I am using PHP and jQuery. Any help would be great.

+13  A: 

Try something like this:

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

then in the jQuery:

$(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();
  });
});
bendewey
Sorry for the early post, finger slipped on the keyboard. Looks like I got a downvote for it.
bendewey
Should this.hide(); be replaced with $(this).hide();
Jose Basilio
I tested this and it works. Just note, you may need to initially hide the views/show the default.
bendewey
@Jose, not from my tests. this as already the wrapped set object.
bendewey
I added a null default option
bendewey
That works beautifully.. gotta love Jquerry, and those who produce examples like this
Bingy
+3  A: 

There are a few different ways you could do this. The simplest is to have a few separate fieldsets, each one containing a single group of fields. Then, in jQuery, dependent on the select-menu's value you can show/hide these fieldsets, e.g.

<fieldset id="f1">
    <input name="something1" />
    <input name="something2" />
    <input name="something3" />
</fieldset>
<fieldset id="f2">
    <input name="something4" />
    <input name="something5" />
    <input name="something6" />
</fieldset>
<select name="fieldset-choice">
    <option value="f1">Fieldset 1</option>
    <option value="f2">Fieldset 2</option>
</select>

<script type="text/javascript">
    jQuery('select[name=fieldset-choice]').change(function(){
        var fieldsetName = $(this).val();
        $('fieldset').hide().filter('#' + fieldsetName).show();
    });

    // We need to hide all fieldsets except the first:
    $('fieldset').hide().filter('#f1').show();
</script>

Note: For the above technique to be entirely unobtrusive you might want to dynamically build the select-menu with the names of all the different fieldsets.


Alternatively you can prefix each fields name with a meaningful prefix, and then hide/show according to that attribute:

<input name="group1-name1" />
<input name="group1-name2" />

<input name="group2-name3" />
<input name="group2-name4" />
<input name="group2-name5" />

<select name="field-choice">
    <option value="group1">Group 1</option>
    <option value="group2">Group 2</option>
</select>

<script type="text/javascript">
    jQuery('select[name=field-choice]').change(function(){
        var groupName = $(this).val();
        $('input').hide().filter('[name^=' + groupName + ']').show();
    });

    // We need to hide all fields except those of the first group:
    $('input').hide().filter('[name^=group1]').show();
</script>
J-P
A: 

To fire up the code on load, just add .change(). As shown below...

  $(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(); }).change(); });

A: 

Hi bendewey,

This is a great and simple bit of code that would help me greatly. Except for that it initially shows all divs. Only after choosing an option does it show/hide as expected. It there a way to initally hide them all?

Thanks in advance, Martin (more of a designer, than a developer)

Martin
A: 

@Martin Try this

`$('#viewSelector').trigger('change');`
Jose
A: 

This script doesn't work in IE7. Works great in Firefox and Chrome. Any suggestions?

Joe
nevermind, i had an extra comma in the jquery code. works great!
Joe