Using jQuery and CoffeeScript in WordPress

If you're properly enqueuing scripts and styles into your WordPress plugins and themes, you may have noticed this doesn't work:

$(document).ready(function(){
  alert( $('.container').length );
});

jQuery

The solution, using jQuery, is this:

jQuery(document).ready(function($){
  alert( $('.container').length );
});

You have to pass the $ to the ready function before you can effectively use it.

CoffeeScript

The CoffeeScript equivalent to this is:

jQuery(document).ready ($) ->
  alert $('.container').length;

Short, simple, but a frustration saver.

Did you learn something or find this article interesting?

If so, why not