2

HTML

more code here...

<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="https://example.com/sites/all/modules/ac_modules/ac_exercise/ac_exercise.js?nkuo2r"></script>

more code here...

<div class="form-item form-type-textfield form-item-time-start">
  <label for="edit-time-start">Time Start </label>
 <input type="text" id="edit-time-start" name="time_start" value="11:28:12" size="4" maxlength="8" class="form-text" />
</div>

more code here...

Javascript - ac_exercise.js

$(document).ready(function(){ 
  $("#edit-time-start").on("change paste keyup", function() {
     alert($(this).val()); 
  });
});

In Chrome's console I get the following error: Uncaught TypeError: undefined is not a function - ac_exercise.js?nkuo2r:1 which is this line of code:

$(document).ready(function(){ 
3
  • 4
    You can wrap your code in (function($){ CODE })(jQuery); Commented Mar 7, 2015 at 16:45
  • That fixed it but why did I have to do that? Is that something different with Drupal 7? I work on a Drupal 6 site at work and I've never had to do that to use jquery.
    – user24363
    Commented Mar 7, 2015 at 16:50
  • 3
    That's because jQuery is namespaced in Drupal 7 to avoid conflicts with using $. So any JS needs to be wrapped in an anonymous closure and pass in jQuery variable if needed. Commented Mar 7, 2015 at 16:59

3 Answers 3

3
(function ($) {
  $(document).ready(function(){

  your code here

  });
})(jQuery);
3

jQuery needs to be wrapped and passed in an anonymous closure like this in Drupal 7.

If you are writing code that needs to fire the document is ready, you could evaluate if you should make it a Drupal behavior or not.

(function ($) {
  $(document).ready(function(){

    $("#edit-time-start").on("change paste keyup", function() {
      alert($(this).val()); 
    });

  });
})(jQuery);
-1

Please check your current JQuery library may not have any conflicts.

However, you can use the JQUERY at the bottom. Please refer this article

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.