You are declaring ShowLoading variable in what seems a different scope, so, you cant access it from another function
This is your code:
$(document).ready(function() {
if(ShowLoading == true){
$("#Loading").show();
}else{
$("#Loading").hide();
}
});
...
if ($.trim(ObjNumber) != ''){
$.post('#URL#', {ObjNumber: ObjNumber}, function(data) {
var ShowLoading = "No"; // this variable is only accessible
// from this scope
$('div#Results-Data').html(data);
});
}
To fix the problem with the scope, I suggest (as the simplest alternative) to declare the variable in a common parent scope, something like that:
var ShowLoading; // declare the variable here
$(document).ready(function() {
if(ShowLoading == true){
$("#Loading").show();
}else{
$("#Loading").hide();
}
});
...
if ($.trim(ObjNumber) != ''){
$.post('#URL#', {ObjNumber: ObjNumber}, function(data) {
ShowLoading = false; // you assign the variable as normal
// this variable can be accessed
// from parent and sibling scopes too
$('div#Results-Data').html(data);
});
}
NOTE: Although there is no variable types on javascript, is preferable where possible to keep consistence on variables of use true/false for boolean, this would make the code a little more readable
WARNING: this solve the scope issue, but still remains another issue: a Race Condition, if your $(document).ready event handler is called before your callback for post, your "show loading" will never be hidden.
To fix this another issue, I suggest to hide the #Loading element in the post callback, this way:
/*var ShowLoading; No need for ShowLoading and it doesnt really help*/
$(document).ready(function() {
$("#Loading").show();
});
...
if ($.trim(ObjNumber) != ''){
$.post('#URL#', {ObjNumber: ObjNumber}, function(data) {
// instead of setting ShowLoading = "No", we can hide the element
// from here
$(document).ready(function() {
// wait for document ready before hiding #Loading element
// to avoid accessing uninitialized DOM
$("#Loading").hide();
});
$('div#Results-Data').html(data);
});
}
NOTE: if you are trying to check if ObjNumber is a number, is preferable to use something like that:
if (!isNaN(ObjNumber)){
...
}