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
+3.7k PHP : Extract part of a string starting from the middle
+15.6k Golang : Aligning strings to right, left and center with fill example
+32k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+17.8k Mac OSX : Homebrew and Golang
+8.1k Golang : Format strings to SEO friendly URL example
+6.4k Golang : Sort words with first uppercase letter
+16k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+11.7k Golang : reCAPTCHA example
+7.5k Golang : Random integer with rand.Seed() within a given range
+17.8k Golang : Archive directory with tar and gzip
+4.5k Unix/Linux : How to open tar.gz file ?
+15.4k Golang : Defer function inside init()