2

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.

ya23
  • 14,226
  • 9
  • 46
  • 43
Malcolm
  • 12,524
  • 28
  • 89
  • 125
  • 1
    Also, what is your sp variable doing in there? You never use it. – Cᴏʀʏ Apr 24 '09 at 03:38
  • 5
    Don't blame jQuery for what might just be your own mistakes – matt b Apr 24 '09 at 03:47
  • 1
    When using jQuery there is no longer a need to use getElementById. use a selector $('#myId') – bendewey Apr 24 '09 at 03:52
  • 1
    Malcolm, you never chose an answer for this question. Did you ever get it solved? If not, I'd like to help you resolve it so visitors can get an answer if the stumble upon this problem/question. If so, you should go ahead and do that. – KyleFarris May 11 '09 at 19:37

7 Answers7

6

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
  • 17,274
  • 5
  • 40
  • 40
  • 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 Apr 24 '09 at 03:56
  • 2
    +1 this is way I would do it in jQuery, I don't like mixing the old with the new. – bendewey Apr 24 '09 at 03:57
  • @Bayard Randel can you link to that doc. I've done performance testing on $('#id :selected') being significantly slower than $('#id').val(); – bendewey Apr 24 '09 at 03:58
  • http://stackoverflow.com/questions/541236/how-can-i-speed-up-jquery-selected-selector – bendewey Apr 24 '09 at 03:59
  • Any real reason to use .bind('change', ...) instead of .change(function()) ? – matt b Apr 24 '09 at 04:04
  • @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 Apr 24 '09 at 04:10
  • @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 Apr 24 '09 at 04:16
2

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>
rcollyer
  • 10,475
  • 4
  • 48
  • 75
Kip
  • 107,154
  • 87
  • 232
  • 265
1

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
  • 10,687
  • 7
  • 27
  • 27
  • 3
    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 Apr 24 '09 at 03:52
  • @KyleFarris, Although your right there is no functional difference in re-wraping a jquery object set. $($($($(sp)))) still works fine. – bendewey Apr 24 '09 at 03:55
  • @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 Apr 24 '09 at 04:07
  • @KyleFarris - good point, I've been doing it this way all along! :) Good to know. – wows Apr 27 '09 at 23:05
1

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
  • 44,326
  • 9
  • 65
  • 80
0

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>

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
  • I think $("#lst").val() should work across browsers. if you're using jquery, *use jquery*. :) – Kip Apr 24 '09 at 03:41
0

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;
DLS
  • 5,313
  • 8
  • 37
  • 50
0

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
  • 9,930
  • 3
  • 42
  • 46