How to call jquery plugin function outside script plugin

I have below script code

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

            function start() {
                //start
            }
            ....

How can I call the start () function outside

(function($) {

Please, help

+4
source share
2 answers

Assuming it start()uses variables / function also defined in its parent anonymous function, the easiest way I can imagine is to create a variable outside of jQuery that you assigned. Then you can call it both inside jQuery and outside jQuery.

Note. The disadvantage of this is that you can only call it start();after the page has finished loading. If you try to call start();before this, it will cause console errors.

Here is an example ...

var start = null;

$(function(){
    var exampleVariable = "hello world";

    start = function() {
        alert(exampleVariable);
    }
    
    // You can call the function as normal here...
    // start();
});

// You cannot call it here, because the page won't have loaded, so start won't be defined
// start();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" onclick="start();" value="Call Function"/>
Hide result
+3

, jQuery , . script javascript, . :

<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<!-- Plugin -->
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<!-- MyFile -->
<script src="/js/MyFile.js"></script>

.

-2

All Articles