views:

4712

answers:

7

This .show and .hide works great in Firefox 3 but not in IE 7. When I click < in the list in IE the span hides but does not show again when I select Between again.

Am I doing something wrong?

    <select id="lst" onchange="onselectchange();">
        <option>Between</option>
        <option>&lt;</option>
    </select>   
    &nbsp;&nbsp;
    <span id="spanAnd">And</span>

   <script type="text/javascript">
       function onselectchange() {
           var lst = document.getElementById('lst');
           var sp = document.getElementById('spanAnd');
           if (lst.value == 'Between') {
               $('#spanAnd').show();
           }
           else {
               $('#spanAnd').hide();
           }
       }
   </script>

EDIT: I tried onclick and onchange.

A: 

Update: Forgive me if this answer is way off; I'm not a jQuery user. These are my thoughts though:

lst.value is not a valid way to access the elements in a select element in IE; at least, I've never seen it done that way (or is that a jQuery trick?)

Do this instead:

if (lst.options[lst.selectedIndex].text == 'Between') { ...

Also notice that I used the "text" property, as this is the value between the <option> tags. The value property is the attribute that's supposed to exist on the <option> element, such as: <option value="0">Between</option>

Cory Larson
I think $("#lst").val() should work across browsers. if you're using jquery, *use jquery*. :)
Kip
+1  A: 

Hi,

Try using jQuery to find and read the elements also. Something like this:

 function onselectchange() {
       var lst = $('#lst');
       var sp = $('#spanAnd');
       if ($(lst).val() == 'Between') {
           $(sp).show();
       }
       else {
           $(sp).hide();
       }
   }
wows
Tested briefly in IE7 and seems to work.
wows
There's no reason (and in fact, I'm surprised it worked) to do '$(sp).show()', for instance, when it is already a jQuery object. You're effectively turning a jQuery object into a jQuery object. You can simply use 'sp.show()'
KyleFarris
@KyleFarris, Although your right there is no functional difference in re-wraping a jquery object set. $($($($(sp)))) still works fine.
bendewey
@bendewey, Yeah, that's what I thought. I vaguely remember doing that quite often when I was first learning jQuery and it never caused me a problem. Will take up more memory though. :-\
KyleFarris
@KyleFarris - good point, I've been doing it this way all along! :) Good to know.
wows
A: 

First of all, to get the selected option text with jQuery is

$("#lst option:selected").text();

Refer to this http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_get_the_text_value_of_a_selected_option.3F if you need more clarification.

Second of all, to get it without a library,

var lst = document.getElementById('lst');
var ind = lst.selectedIndex;

var text = lst.options[ind].text;
Dhana
A: 

This works in IE and Firefox

    <select id="lst" onchange="onselectchange();">
        <option>Between</option>
        <option>&lt;</option>
    </select>   

    <span id="spanAnd">And</span>

   <script type="text/javascript">
       function onselectchange() {
           if ($('#lst :selected').text() == "Between") {
               $('#spanAnd').show();
           }
           else {
               $('#spanAnd').hide();
           }
       }
   </script>
Bayard Randel
+6  A: 

Use the jQuery bind method on page load... it will help abstract your interaction logic from your presentation logic.

<script type="text/javascript">
    $(function() {
        $('#lst').bind('change',function() {
            var sp = $('#spanAnd');
            if($(this).val() == 'Between') {
               sp.show();
            } else {
               sp.hide();
            }
        });
    });
</script>

That's about as simple as it gets...

NOTE: I just noticed that you are checking for the text of the option and not the value of the select box. Frankly, I think that's silly but if you need to do that for some reason, you can do this:

<script type="text/javascript">
    $(function() {
        $('#lst').bind('change',function() {
            var sp = $('#spanAnd');
            var selected_text = $(this).find('option:selected').html();
            // Possibly faster ways (thanks to bayard and bendewey):
            /*
            var selected_text = $("#lst option[value=" + $('#lst').val() + "]").text();
            // ... or ...
            var selected_text = $("option[value="+$('#lst').val()+"]",this).text();
            // ... or even... 
            var selected_text = $(this).children('option[value="+$('#lst').val()+"]").text();
            */ 
            if(selected_text == 'Between') {
               sp.show();
            } else {
               sp.hide();
            }
        });
    });
</script>

To make my original suggestion work, you would need to change your HTML to:

<select id="lst" onchange="onselectchange();">
    <option value="Between">Between</option>
    <option value="&lt;">&lt;</option>
</select>

... which, as I said, kinda seems like what you'd want to do anyways.

KyleFarris
that's awesome, no need at all to handle events in markup!
Bayard Randel
btw, I think your method is the best posted, however the jquery docs suggest using ($('#id :selected').text() to get the option text as opposed to the value with .val(). Presumably if there is no option value specified, the value is considered equivalent to the option text.
Bayard Randel
+1 this is way I would do it in jQuery, I don't like mixing the old with the new.
bendewey
@Bayard Randel can you link to that doc. I've done performance testing on $('#id :selected') being significantly slower than $('#id').val();
bendewey
http://stackoverflow.com/questions/541236/how-can-i-speed-up-jquery-selected-selector
bendewey
Any real reason to use .bind('change', ...) instead of .change(function()) ?
matt b
@matt b, Because I like it better, haha. I went into more detail about this in one of my answers: http://stackoverflow.com/questions/756547/the-difference-between-assigning-event-handlers-with-bind-and-each-in-jquery/756634#756634
KyleFarris
@bendewey, .val() and .text() should return different values (assuming an option value is set). The post you linked offers a nice solution that avoids using the selector though:$( "#id option[value=" + $( "#id").val(); + "]").text();
Bayard Randel
+1  A: 

Try this (putting together some things I left in comments to two other answers):

<script type="text/javascript">
$(function() {
  $("#lst").change(function() {
    if ($("#lst).val() == 'Between') {
      $('#spanAnd').show();
    }
    else {
      $('#spanAnd').hide();
    }
  })
});
</script>
Kip
+1  A: 

There is alot of problems you have there.

not wrapping in $(document).ready(); The element may not be loaded when the script is executed.

Using a variable name that starts with a number. (bad style and not sure that is even allowed)

Using the variable name that corresponds to an element's ID.

(Old versions of IE allowed you to use ID.whatever without calling document.getElementById())

Mixing jQuery / Standard DOM.

Calling val() gives you the option value attribute, not the text of the option

   <select id="first">
        <option value="between">Between</option>
        <option>&lt;</option>
    </select>   

    <span id="spanAnd">And</span>

   <script type="text/javascript">
      $(document).ready(function(){

         $("#first").change(function(){   

            if ($("#first").val() == 'between') {
                $("#spanAnd").show();
            }   
            else {
               $("#spanAnd").hide();
            }   

        });
       });
   </script>
Chad Grant