user2301210
user2301210

Reputation: 409

How could I call function in a jQuery plugin?

How could I call the function 'gotopage' below in javascript?

I used 'gotopage(5);',but the browser points out the function is not defined.So what is the correct answer to call the function in others' jQuery plugin?

;(function ($) {

$.fn.booklet = function (options, param1, param2) {

  //..............
};

function Booklet(inTarget, inOptions) {

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // PUBLIC FUNCTIONS
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    return {
        init: init,
        enable: enable,
        disable: disable,
        destroy: destroy,
        next: next,
        prev: prev,
        gotopage: function (index) {
            //.............
            goToPage(index);
        },
        add: addPage,
        remove: removePage,
        option: function (name, value) {
           //.............
        }
    }
}

// define default options
$.fn.booklet.defaults = {
     //...........
}

})(jQuery);

Upvotes: 0

Views: 173

Answers (3)

Thi49
Thi49

Reputation: 358

Hope this helps ..i haven't tried testing it

 `(function ($) {

$.fn.booklet = function (options, param1, param2) {

    //..............
};

function Booklet(inTarget, inOptions) {

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // PUBLIC FUNCTIONS
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    return {
        init: init,
        enable: enable,
        disable: disable,
        destroy: destroy,
        next: next,
        prev: prev,
        gotopage: function (index) {
            //.............
            $.fn.goToPage(index);
        },
        add: addPage,
        remove: removePage,
        option: function (name, value) {
            //.............
        }
    }
}

// define default options
$.fn.booklet.defaults = {
    //...........
}
$.fn.goToPage=function(index){
 //function is now public
};

})(jQuery);

`

Upvotes: 0

Oliver Schaub
Oliver Schaub

Reputation: 71

"goToPage" is a function "inside" another function "Booklet" and this again is inside another anonymous function (as far as I can see).

You can't call this function directly from outside this function. You need to call if from the same scope.

And here is the solution quickly found by reading the Plugin Documentation:

$('#custom-goto').click(function(e){
    e.preventDefault();
    $('#mybook').booklet("gotopage", "end");
    });

Upvotes: 1

Thi49
Thi49

Reputation: 358

goToPage(index) is a private function it couldn't be called outside the plugin.Try creating function globally using $.fn.

$.fn.goToPage=function(index){ }

Upvotes: 0

Related Questions