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.4k Golang : Rot13 and Rot5 algorithms example
+11.4k Golang : Change date format to yyyy-mm-dd
+15.4k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+13.5k Golang : Set image canvas or background to transparent
+18.4k Golang : Send email with attachment
+7.9k Findstr command the Grep equivalent for Windows
+19.2k Golang : Fix cannot download, $GOPATH not set error
+27.4k PHP : Convert(cast) string to bigInt
+8.8k Golang : Populate or initialize struct with values example
+5.2k Golang : Generate Interleaved 2 inch by 5 inch barcode
+19.3k Golang : How to count the number of repeated characters in a string?