How to call a function declared in $ (document) .ready ()?

I have the following code:

$(document).ready(function() {
    var refresh = function() {
        alert('doing!');
    }
}

How to call a function refresh()out $(document).ready()? Anywhere in jQuery functions?

eg.

$('#el').click() {
    document.ready().refresh();
}
+3
source share
1 answer

It is impossible to call a method defined in a more local scope from another, you need to either save a link to it or declare it in / in a higher scope, for example:

$(document).ready(function() {
  window.refresh = function() {
    alert('doing!');
  };
});

Or:

var refresh = function() {
  alert('doing!');
};
$(document).ready(function() {
  //other code...
});
+7
source

Source: https://habr.com/ru/post/1782906/


All Articles