Search code examples
javascriptmootools

MooTools-Core: uncaught TypeError: undefined is not a function


I have this code function:

window.addEvent('domready', function() {
    $('myForm').addEvent('submit', function(e) {
        var content = CKEDITOR.instances['comment'].getData();
        $('comment').setProperty('value',content)
        e.stop();
        var log = $('log_res').empty().addClass('ajax-loading');
        this.set('send', {onComplete: function(response) { 
            log.removeClass('ajax-loading');
            log.set('html', response);
        }});
        this.send();
    });
});

im getting:

Uncaught TypeError: undefined is not a function

currently using: mootools-core: 1.5.1


Solution

  • putting comment into an answer.

    it looks as if it's trying to call .addEvent() on the chained result of $ - which normally is an alias for document.id. However, if Mootools gets loaded after jQuery, it won't define the $ and chances are, you are calling it on the jQuery.fn prototype.

    use a closure pattern around your mootools code:

    (function($){
        $('myForm').addEvent('submit', function(e) {
            var content = CKEDITOR.instances['comment'].getData();
            $('comment').setProperty('value',content)
            e.stop();
            var log = $('log_res').empty().addClass('ajax-loading');
            this.set('send', {onComplete: function(response) { 
                log.removeClass('ajax-loading');
                log.set('html', response);
            }});
            this.send();
        });
    }(document.id));
    

    this ensures the value of $ in your mootools code is exactly what you need it to be.

    you can also look at jQuery.noConflict() to prevent jQuery from exporting to $ and sticking to jQuery only.