3

I don't know what the hell is going on.

Why isn't my simple javascript code working? Been here for hours. I have a bunch of other javascript, was thinking if it's interrupting or something?

Trying to through a tutorial, then I got stuck at the first line:

$('#name').keyup(function() {
    alert('alert');
});

I don't know the problem.. in fiddle it's all working of course http://jsfiddle.net/pgWtK/1/

I tried putting it inside the head with document ready but that doesn't help so, any clues?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    Can you post your complete code? – Alessandro Minoccheri Nov 16 '12 at 15:53
  • sure. Jquery is now running inside the document ready function and it's still the same result (see vote.js). I can post the page I am trying to get it work in. [link](http://epicesports.com/vote/) .I am so thankful that you guys posted so many guidelines for me. – 今際のアリス Nov 16 '12 at 16:32
  • I'm voting to leave this question open but I want you to append links and whatnot to the question if you would. – MrBoJangles Nov 16 '12 at 18:36

5 Answers5

4

You need to put it in $(document).ready(function(){ so that the DOM elements are available to jquery before use, in your case element with id name for binding keyup event.

Also make sure you have included jquery tag. This article will guide you how to use jquery.

$(document).ready(function(){    
     $('#name').keyup(function() {
        alert('alert');
     });
})
Adil
  • 146,340
  • 25
  • 209
  • 204
2

Two things might happen, you dont have correctly loaded Jquery Library or you have not putted that code inside document ready

add jquery in your head example:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

jquery inside document ready

$(function(){
   $('#name').keyup(function() {
      alert('alert');
   });
});
KoU_warch
  • 2,160
  • 1
  • 25
  • 46
2
  1. Make sure FireBug or an alternative is installed
  2. Check jQuery is loading in the Network panel of FireBug or alternative. If you're loading from a cdn and testing locally, make sure there's a fallback if it's not loading
  3. Make sure your jQuery code is wrapped in a $(document).ready(...) call
  4. Use the following code in your console of FiregBug or alternative to make sure that the event has been bound to the element:

    $('#name').data('events');

LeonardChallis
  • 7,759
  • 6
  • 45
  • 76
0

wrap up your code inside the document ready

$(function(){
 //your code

});

in jsfiddle it done by default but if you select the no-wrap(head) option it would not wrap your js code inside the ready handler see it for yourself.

It's always a good practice to place your js code inside the ready handler so that it is ensured before all the js glitter happens the DOM is rendered, or you can place the js script at the end of your markup

Community
  • 1
  • 1
Rafay
  • 30,950
  • 5
  • 68
  • 101
0

The problem is in the file vote.js change the code wth this code you have miss $(

$(document).ready(function(){
   $(function(){
     $('#name').keyup(function() {
     alert('alert');
     });
  });
})
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
  • Okay BIG thanks to you and everyone! I am so stupid! This with combination with document ready made everything work as it should. Thank you again and sorry for my bad skill. – 今際のアリス Nov 16 '12 at 16:51