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
+6.3k Golang : Create zip/ePub file without compression(use Store algorithm)
+4.6k Golang : What is StructTag and how to get StructTag's value?
+17.3k Golang : Example for RSA package functions
+4.3k Gogland : Datasource explorer
+12.9k Golang : syscall.Socket example
+5k Golang : Experimenting with the Rejang script
+17.4k Unmarshal/Load CSV record into struct in Go
+7.1k Golang : Number guessing game with user input verification example
+24.4k Golang : How to write CSV data to file
+12.3k Golang : Set image canvas or background to transparent
+14.2k Golang : Delete certain files in a directory
+8.7k Golang : Check if user agent is a robot or crawler example