jquery plugin, boolean callback parameter

I am trying to pass a boolean parameter to a callback from inside a jquery plugin but parameter is always undefined.

The onSwitch callback parameter should alternate between true and false each time the link is clicked. Using a debugger I can see that the value passed to callback call function is properly defined as true or false but inside the callback implementation it turns to undefined.

I have tried looking at several other similar questions like this this and this but cannot seem to get this to work.

This is my plugin definition:

(function ($) {
    $.fn.switcherButton = function (options) {
        // Set the default options
        var settings = $.extend({},$.fn.switcherButton.defaults, options);

        this.click(function () {
            $.fn.switcherButton.switched = !$.fn.switcherButton.switched;
            settings.onSwitch.call($.fn.switcherButton.switched);
        });
        return this;
    };
    // Plugin defaults – added as a property on our plugin function.
    $.fn.switcherButton.defaults = {
        onSwitch: function() {}
    };
    $.fn.switcherButton.switched = false;
}(jQuery));

HTML:

<a id="switchTest" href="#">switch</a>

plugin initialization:

$("#switchTest").switcherButton({
    onSwitch: function(switched){
        if(typeof switched === "undefined")
            alert("callback param = undefined");
        else
            if(switched)
                alert("callback param = true");
            else
                alert("callback param = false");
    }
});

I have created a jsfiddle of the problem here.

Answers


Change settings.onSwitch.call($.fn.switcherButton.switched) to settings.onSwitch($.fn.switcherButton.switched)

Call is used to set this and not the actual argument to the function


Need Your Help

How to import Macapi Core foundation and other Macapi namespaces in Fire Monkey HD app?

delphi firemonkey rad-studio

I want to use Macapi Core foundation and other Macapi namespaces with in Fire monkey HD application.How can I import them.I am trying to include it with in uses clause under interface section but it

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.