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
+31.8k Golang : Convert an image file to []byte
+30.8k Golang : Interpolating or substituting variables in string examples
+11.5k Android Studio : Create custom icons for your application example
+8.5k Android Studio : Import third-party library or package into Gradle Scripts
+12.1k Golang : Pagination with go-paginator configuration example
+26.8k Golang : Find files by extension
+10.5k Golang : Allow Cross-Origin Resource Sharing request
+18.1k Golang : Put UTF8 text on OpenCV video capture image frame
+6.8k Default cipher that OpenSSL used to encrypt a PEM file
+22.1k Golang : Repeat a character by multiple of x factor
+15.2k Golang : Get timezone offset from date or timestamp