0

I have a simple jquery plugin. It is working fine when i pass a value. But not working when a function returns a value. It's shows the function defenition.

Please try this in Jsfiddle

(function ( $ ) {
    $.fn.greenify = function( options ) {

        var settings = $.extend({
            // These are the defaults.
            color: "#556b2f",
            backgroundColor: "white",
            selectedID:function(){}    
        }, options );

        alert(settings.selectedID);
    };
}( jQuery ));


//Plugin Call
$( "div" ).greenify({
    color: "orange",
    //selectedID:6  it is working fine
    selectedID:function (){return 5}
});
2
  • You're not executing the function, just passing a function object. Commented Oct 18, 2013 at 6:32
  • Why are you wanting to pass a function there? Why is the default value specified for selectedID a function? The name "selectedID" implies that it will be an ID (number or string), not a function, so if you want to use a function when you call .greenify() you'd need to call your function yourself and pass the return. If the .greenify() plugin is expecting selectedID to be a function then the plugin code would need to treat it as such and call it by adding parentheses (alert(settings.selectedID())). Commented Oct 18, 2013 at 6:34

3 Answers 3

4

You are passing a function reference to alert() when you call alert(settings.selectedID), instead you need to invoke the function referred by settings.selectedID and pass the value returned by it to alert()

So try

alert(settings.selectedID());

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

You are missing "()" here

alert(settings.selectedID);

So ideally you have to invoke the function to work it properly

alert(settings.selectedID());

Comments

0

settings.selectedID is a function

alert(settings.selectedID());

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.