views:

151

answers:

4

In FF this hides all divs and then shows the id that was selected from the '#rule_rule_type' menu, which is the expected behavior. In IE 8 it does not hide all div id's:

<script type="text/javascript" charset="utf-8">
  (function($){
    $('#rule_rule_type').change(function() {
      $('#allowed_senders, #blocked_senders, #blocked_character_set, #custom').hide();
      var id = $(this).val();
      $('#' + id).show();
    });
  })(jQuery); 
</script>

However, this DOES work in IE 8:

<script type="text/javascript" charset="utf-8">
  (function($){
    $('#rule_rule_type').change(function() {
      $('#allowed_senders').hide();
      $('#blocked_senders').hide();
      $('#blocked_character_set').hide();
      $('#custom').hide();
      var id = $(this).val();
      $('#' + id).show();
    });
  })(jQuery); 
</script>

This is messy. How can I clean this up to be more concise and still work in IE 8?

Thanks,
Chip Castle
http://invoicethat.com

A: 

I haven't seen your HTML, but the way you've named and referenced your IDs above suggests that you may be using the same ID more than once. Just a guess.

Ken Redler
The IDs sure look unique to me. What about his code suggests otherwise?
patrick dw
A: 

Have you tried validating your HTML document? Don't know if this works, but one more way...

PeterWong
A: 

What happens if you add a class to all the DIVs you're showing and hiding, and then use this slightly rephrased code? In this example, you'd add "classofalldivs" as a class to those divs you want to affect:

  (function($){
    $('#rule_rule_type').change(function() {
      var id = $(this).val();
      $('#' + id).show();
      $('.classofalldivs:not(' + '#' + id+ ')').hide();
    });
  })(jQuery); 

This uses the "not" pseudo-selector to hide everything but the div you're showing, and stands a chance of working in both IE and FF.

Having said that, I'd agree with the other suggestions that your original code doesn't seem to look wrong, so there may be other strange things going on. Can you post a complete and full example of your problem so others can reproduce it?

Matt Gibson
+1  A: 

I'm running your sample just fine in IE8 and Chrome. Is it different in some way?

<!DOCTYPE html>
<html lang="en">
<head>
    <title>jQuery Sandbox</title>
</head>
<body>

    <select id='rule_rule_type'>
        <option value="allowed_senders">allowed_senders</option>
        <option value="blocked_senders">blocked_senders</option>
        <option value="blocked_character_set">blocked_character_set</option>
        <option value="custom">custom</option>
    </select>

    <hr />

    <div id="allowed_senders">#allowed_senders</div>
    <div id="blocked_senders">#blocked_senders</div>
    <div id="blocked_character_set">#blocked_character_set</div>
    <div id="custom">#custom</div>

    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript">

        $(function () {

            $('#rule_rule_type').change(function () {

                $('#allowed_senders, #blocked_senders, #blocked_character_set, #custom').hide();

                var id = $(this).val();
                $('#' + id).show();

            });

        });

    </script>
</body>
</html>
AndrewDotHay