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.5k Golang : Lock executable to a specific machine with unique hash of the machine
+13.7k Golang : Compress and decompress file with compress/flate example
+8.1k Your page has meta tags in the body instead of the head
+8.6k Golang : Find network service name from given port and protocol
+21.4k SSL : How to check if current certificate is sha1 or sha2
+7.1k Linux : How to fix Brother HL-1110 printing blank page problem
+3.8k Detect if Google Analytics and Developer Media are loaded properly or not
+5.8k Golang : How to verify input is rune?
+24.9k Golang : Generate MD5 checksum of a file
+21.8k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+7.9k Golang : How To Use Panic and Recover
+42.9k Golang : Get hardware information such as disk, memory and CPU usage