0

I want to pass an anonymous function as a callback, then call it. I am probably missing something simple, but I just get the error 'Uncaught type error - callback is not a function'. This is what I am doing - (using jQuery) - I pass the callback as an anonymous function when creating a new object:

$('#someid').alphaColorPicker({
            callback: function() {
                console.log("called")
            }
        });

Then I call it at some point (or try to):

$.fn.alphaColorPicker = function(callback) {

    ...
    ...

    callback(); //this throws the error
}

How do I correctly call the callback function? Thanks.

0

4 Answers 4

3

Look at the value you are sending:

{ callback: function () { ... } }

That isn't a function.

It is an object with a property called callback which is a function.

Therefore:

callback.callback();

Or you could pass an actual function instead of an object:

$('#someid').alphaColorPicker(function() { console.log("called") });
Sign up to request clarification or add additional context in comments.

Comments

0

You are not directly passing the function, you are passing an object which has callback property

make it

$.fn.alphaColorPicker = function(options) {

    ...

    options.callback(); //this throws the error
}

Comments

0

The thing you pass into the alphaColorPicker is not a callback function but rather a object containing a value that is a callback.

{ // When putting it within {} its a new object.
  // Where 'callback' is a member/key of the object.
  callback : function() {
    console.log("called");
}

If you instead pass the function directly:

$('#someid').alphaColorPicker(function() {
    console.log("called");
});

You can call it right away via callback();. If you wish to keep it as an object, you can call it by calling the member of the object instead of trying to call the object as a function:

callback.callback();

2 Comments

Thank you, I just kind of figured that out but your answer does explain why I was not getting anywhere - should have used callback.callback(), as you pointed out.
@ardmark, feel free to mark an answer as correct (and/or upvote) if any of them helped you to solve the issue.
-2

Found the problem, should not have passed the function as

callback: function() {
                console.log("called")
            }

But just like this:

    $('#' + boxID).alphaColorPicker(function() {
            console.log("hello")
    });

And if I want to pass parameters in, I can do it like this:

$('#' + boxID).alphaColorPicker({x:styleName, y:id, callback:function() {
        //do something
}});

But I am confused about how to pass parameters in / out like in jQuery event handlers such as:

$("#"+boxID).alphaColorPicker({x:styleName, y:this.inputID}, function(e){}

});

How do you access the callback here? In my example, in alphaColorPicker callback.x and callback.y are obviously available, but the callback function is passed as an object I can't see how to call it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.