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:

  alert( $('.container').length );


The solution, using jQuery, is this:

  alert( $('.container').length );

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


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