Craig Lafferty
Craig Lafferty

Reputation: 791

Jquery plugin using deferred object not working

(function ( $ ){            
    $.fn.customAlert = function(callback ){
        buttonClicked().then(function(){
            if (typeof callback == 'function'){
                callback.call(this);
            }
        }, function(){
            alert("didn't work");
        }).always(function(){               
            });
    };    
}( jQuery ));

I'm trying to create a custom prompt and this is what I have so far. The buttons show up and I call $.customAlert(...); in another part of my javascript but the buttons don't generate any response.

Uncaught TypeError: Object function (e,t){return new x.fn.init(e,t,r)} has no method 'customAlert'

Upvotes: 0

Views: 40

Answers (1)

Jacob
Jacob

Reputation: 78848

You're creating customAlert in $.fn. That is used for adding methods to jQuery selections. This is why calling $.customAlert(...); will not work. If you want to be able to invoke customAlert in this way, you should assign it to $, not $.fn. Otherwise, you would have to invoke it with $('some selector').customAlert.

Upvotes: 1

Related Questions