tags:

views:

26

answers:

2

Hello, I have a form and I'd like to show a div based on the entered text in a text field. The script I'm using works just fine on my latest version of chrome but it doesn't work on IE or any of my members browswers. What they heck is wrong with the script?

    <script>
$('document').ready(function(){
$('#gname').change(function() {
($(this).val() == "NBA 2k10") ? 
$('#number').slideDown('fast') : $('#number').hide();
})
});
</script>
+1  A: 

$("document") isn't a valid selector in other browsers, you need $(document), like this overall:

$(document).ready(function(){
  $('#gname').keyup(function() { //probably want keyup instead of change here
    if ($(this).val() == "NBA 2k10") 
      $('#number').slideDown('fast')
    else 
      $('#number').hide();
  });
});

You can test it here.

$(document).ready(function(){ can also just be $(function(){. $("document") doesn't work because it's looking for <document> element, which doesn't exist.

The if/else change is just to make it a bit cleaner overall, it's somewhat preference, but generally speaking your conditional statements shouldn't have side effects, that's not their intention. You can use them that way, but it's better to be explicit, at least in my opinion.

Nick Craver
thanks for responding bro, but that's not working
John Sims
@John - I added a demo to show it in action, that you probably *want* is the `keyup` event instead of `change` (which won't fire until you leave the textbox). That's probably your current issue, that the `change` event doesn't fire when you think it does :)
Nick Craver
Keyup, worked. beautiful. Let me know what your Goodiiz.com member id is please I'll send you 3 goodiiz chips for helping me out.
John Sims
@John - I don't use it, but thanks :) Just be sure to accept answers if they resolve your issue, it'll help you get better/faster answers in the future :)
Nick Craver
patrick dw
@patrick - Hah I never noticed, actually it doesn't matter what the selector is at all: `$("ImNotARealElement").ready(function() { alert('hi'); });`, good catch.
Nick Craver
Nick - Wow. No kidding. You can even have an empty object. Makes sense I guess since it's just setting the code aside until the `body` is loaded.
patrick dw
Sorry to clutter your answer with more of this, but just wanted to say that in the interest of not creating unnecessary jQuery objects, perhaps it is better to call `$.fn.ready(func...)` instead of the common versions. And +1 since it looks like you're not getting your Accept tonight. :o)
patrick dw
@patrick - Thanks :) I think the `$(function)` version is fine for this as well, it's what I always use, and it's a direct shortcut without the extra wrapper object: http://github.com/jquery/jquery/blob/master/src/core.js#L159
Nick Craver
A: 

It probably fires in IE only after the input has lost focus. If you want to detect text change (cross-browser) before the focus on the input is lost, try keyup or keypress.

$(document).ready(function(){
  $('#gname').keypress(function() {
    if ($(this).val() == "NBA 2k10") 
      $('#number').slideDown('fast')
    else 
      $('#number').hide();
  });
});
SimpleCoder