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
+5.8k Javascript : How to refresh page with JQuery ?
+8.9k Yum Error: no such table: packages
+6.8k Golang : Output or print out JSON stream/encoded data
+16.4k Golang : convert string or integer to big.Int type
+14.7k Golang : Overwrite previous output with count down timer
+7.2k Golang : Array mapping with Interface
+8.8k Golang : Combine slices but preserve order example
+9.6k Golang : Qt Yes No and Quit message box example
+3.9k Golang : Switch Redis database redis.NewClient
+5.5k Gogland : Datasource explorer
+13.4k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+24.2k Golang : Find biggest/largest number in array