今際のアリス
今際のアリス

Reputation: 354

Keyup doesn't respond

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?

Upvotes: 3

Views: 261

Answers (5)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

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');
     });
  });
})

Upvotes: 0

Adil
Adil

Reputation: 148130

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');
     });
})

Upvotes: 4

Rafay
Rafay

Reputation: 31033

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

Upvotes: 0

LeonardChallis
LeonardChallis

Reputation: 7783

  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');

Upvotes: 2

KoU_warch
KoU_warch

Reputation: 2150

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');
   });
});

Upvotes: 2

Related Questions