- Listen to a "Keyup" in an input field
- Define "enableSubmitButton" as an array
- Do some stuff
- Add an array element to the "enableSubmitButton" array
- From the current input field I m searching the parent form, and then I loop all input fields
- In this loop, I make a request to the Server
- In the "onreadystatechange" function I push another element into the "enableSubmitButton" array
The problem is, that the element that I have pushed within the "onreadystatechange" is not really in the array. When I use the console.log() to view the array, the element is visible, but if I use the "array.length" function, the array element is not included :-O
$('.checkEmail, .checkPwd, .checkPwdC').bind("keyup", function() {
//define enableSubmitButton as an array
var enableSubmitButton = [];
//loop each input field in the form
$(this).parents("form").find(":input").each(function(index,data){
//do some "if then else" and other stuff
...
enableSubmitButton.push(true);
...
// Now I make a request to the server with Ajax
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if(xmlhttp.responseText == "doesntExist"){
enableSubmitButton.push(true);
}else{
enableSubmitButton.push(false);
}
}
};
//Request
xmlhttp.open("GET", "ajax.php?ajaxCase=cuna&userName="+$('#fNewAccount input.checkAvailability').val(), true);
xmlhttp.send();
});
// PROBLEM
// and here we have the problem. To debugg, i use the console.log() function as follow
var okForSubmit = true;
console.log(enableSubmitButton);
console.log("array length: "+enableSubmitButton.length);
for(var i = 0 ; i < enableSubmitButton.length ; i++){
if(enableSubmitButton[i] == false){
okForSubmit = false;
}
var newTime = Math.floor(Math.random() * (100 - 1 + 1)) + 1;
console.log(i+" - "+enableSubmitButton[i]+" - "+newTime+" - "+okForSubmit);
}
});
Here is the console.log() output:
(4) [true, true, true, true]
0: true
1: true
2: true
3: true
4: false
length: 5
__proto__: Array(0)
array length: 4
0 - true - 54 - true
1 - true - 19 - true
2 - true - 51 - true
3 - true - 94 - true
Any Ideas?