Search code examples
jqueryjquery-pluginsjquery-callback

arguments for callback in jquery plugin are undefined


How do you call a function function defined outside a plugin but using arguments inside the plugin. I have it where the plugin is calling the function but the parameters are undefined. Inside the plugin the arguments has values.

(function ($) {
    $.fn.myPlugin = function (options) {
        var defaults = {
            type: 0,
            callBack: function () { }
        };
        var settings = $.extend({}, defaults, options);
        this.find('.list-item').click(function (e) {
            e.preventDefault();
            var txt = $(this).attr('href');
            var id = txt.substring(1, txt.length);
            settings.callBack.call(settings.type, id);  // populated here
        });
        return this;
    };
} (jQuery));

function loadData(type, id) {
    // type and id are undefined
}

<script type="text/javascript">
     $(document).ready(function () {
        $("#mydiv").myPlugin({ 
            type: 190, 
            callBack: function(){ loadData();}
         });
     });
</script>

Solution

  • What if you replace

    callBack: function(){ loadData();}
    

    By

    callBack: loadData
    

    Hope that helps