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
+10.4k Golang : Text file editor (accept input from screen and save to file)
+7.2k Golang : How to setup a disk space used monitoring service with Telegram bot
+10.2k Golang : Check if user agent is a robot or crawler example
+10.6k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+21.5k Golang : Clean up null characters from input data
+5.4k Python : Convert(cast) string to bytes example
+24.3k Golang : Upload to S3 with official aws-sdk-go package
+14.3k Golang : Check if a file exist or not
+11k Golang : Command line file upload program to server example
+5.8k Unix/Linux/MacOSx : Get local IP address
+6.1k Facebook : How to force facebook to scrape latest URL link data?
+31k Golang : Download file example