JQuery : Calling a function inside Jquery(document) block
Problem :
Saw this error message in the browser's console while attempting to call a function inside JQuery(document) section resulted in this error message :
Uncaught ReferenceError: callFunctionInsideJquery is not defined
So, how does one call a function inside JQuery(document) section ?
Solution :
Enable the function be called globally. Add the JQuery window
reserve word in front of the function you want to make it accessible globally. See :
<script>
function pressButton() {
callFunctionInsideJquery("Yes, I'm here!");
}
jQuery(document).ready(function($) {
// private function inside JQuery(document) block
function callFunctionInsideJquery(msg) {
$('#error').html(msg).hide().fadeIn(800);
}
}
);
</script>
to
<script>
function pressButton() {
callFunctionInsideJquery("Yes, I'm here!");
}
jQuery(document).ready(function($) {
// public function inside JQuery(document) block accessible globally with window
window.callFunctionInsideJquery = function(msg) {
$('#error').html(msg).hide().fadeIn(800);
}
}
);
</script>
See also : Javascript : How to refresh page with JQuery ?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.2k Restart Apache or Nginx web server without password prompt
+10.6k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+21.3k Golang : Get password from console input without echo or masked
+7.2k Golang : Gorrila mux.Vars() function example
+7.7k Javascript : Push notifications to browser with Push.js
+27.8k Golang : dial tcp: too many colons in address
+8.6k Linux/Unix : fatal: the Postfix mail system is already running
+17.8k Convert JSON to CSV in Golang
+7.6k Golang : Rot13 and Rot5 algorithms example
+12.2k Golang : Sort and reverse sort a slice of runes
+5.3k PHP : See installed compiled-in-modules
+12.1k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example