Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call function in jquery plugin?

(function($) { 
    $.fn.top_islides = function(){
        var ajax_init = function(){
            init_islides();
            setTimeout(function(){picmove()},300);
        };
//.....
    };  
})(jQuery);

call it in doucument ready in another file

$('#top_slides').top_islides();
$('#top_slides').top_islides().ajax_init();

I thought it should work ,I got an error, what's the problem?

like image 518
FatDogMark Avatar asked Nov 25 '12 07:11

FatDogMark


People also ask

How do you call a function within a function in jQuery?

function someFunction() { //do stuff } $(document). ready(function(){ //Load City by State $('#billing_state_id'). live('change', someFunction); $('#click_me'). live('click', function() { //do something someFunction(); }); });

What is the correct way to call the jQuery library in HTML?

You need to use the script tag. As almost everything we do when using jQuery reads or manipulates the document object model (DOM), we need to make sure that we start adding events etc. as soon as the DOM is ready. If you want an event to work on your page, you should call it inside the $(document).

What are jQuery plugins?

A jQuery plugin is simply a new method that we use to extend jQuery's prototype object. By extending the prototype object you enable all jQuery objects to inherit any methods that you add. As established, whenever you call jQuery() you're creating a new jQuery object, with all of jQuery's methods inherited.


1 Answers

Do it like this:

(function($) {
    //Assuming $.fn.top_islides is defined
    $.fn.top_islides.ajax_init = function(){
        init_islides();
        setTimeout(picmove,300);
    };
 //.....
})(jQuery);

Or

(function($) { 
    $.fn.top_islides = function(){
        var ajax_init = function(){
            init_islides();
            setTimeout(picmove,300);
        };
        return {
            ajax_init: ajax_init
        };
    });
     //.....
})(jQuery);
like image 80
Derek 朕會功夫 Avatar answered Oct 13 '22 22:10

Derek 朕會功夫