How to Use jQuery in Grails 3.0

Viewed 5108

I am trying to figure out how to get jQuery running in my Grails 3.0 application. I attempted adding the jQuery dependency like so in my build.gradle

runtime "org.grails.plugins:jquery:1.11.1"

When I did that, the application failed to build. I have not changed anything in my application.js file, so it still looks like this:

//= require jquery-2.2.0.min
//= require bootstrap
//= require_tree .
//= require_self

if (typeof jQuery !== 'undefined') {
    (function($) {
        $('#spinner').ajaxStart(function() {
            $(this).fadeIn();
        }).ajaxStop(function() {
            $(this).fadeOut();
        });
    })(jQuery);
}

Inside of my gsp pages I have tried both:

<g:javascript>
          $( document ).ready(function() {

                $( "#a" ).click(function( event ) {

                     alert( "Thanks for visiting!" );

                 });

           });
</g:javascript>

and

<g:javascript library='jquery'>
            (function($){
                $( document ).ready(function() {

                    $( "#a" ).click(function( event ) {

                        alert( "Thanks for visiting!" );

                    });

                });
            })(jQuery);
</g:javascript>

The first resulted in an error stating that '$' was not defined and the second said that jQuery was not defined. So how am I supposed to run jQuery inside of a gsp page?

1 Answers
Related