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
+27.8k PHP : Count number of JSON items/objects
+7.3k Golang : Check if one string(rune) is permutation of another string(rune)
+38.1k Golang : Read a text file and replace certain words
+7.3k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+16.7k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+7k Golang : Calculate BMI and risk category
+5.2k Golang : Customize scanner.Scanner to treat dash as part of identifier
+26.9k Golang : Find files by extension
+6.2k Linux/Unix : Commands that you need to be careful about
+17.6k Golang : Find smallest number in array
+13.8k Golang : Check if an integer is negative or positive
+8.5k Golang : How to check variable or object type during runtime?