Reputation: 102
It says 'Uncaught TypeError: Cannot read property 'push' of undefined' I feel im not intializing the array in the correct way please help.
var validationInfo = new Array();
var currentStep = 1;
validationInfo["step1"].push({ elementId: "txtsearchBar", validateFunc: isEmpty });
validationInfo["step2"].push({ elementId: "txtdetails", validateFunc: isEmpty });
function isEmpty(element) {
//alert(element);
return $(element).val() == undefined || $(element).val() == "";
}
function isCurrentSubmissionValid(stepId) {
var isOK = true;
alert(validationInfo);
for (var i = 0; i < validationInfo[stepId].length; i++) {
var element = $("#" + validationInfo[i].elementId);
var validationFunction = validationInfo[i].validateFunc;
if (!validationFunction(element)) {
element.addClass("has-error").removeClass("has-success");
isOK = false;
} else
{
element.addClass("has-success").removeClass("has-error");
}
}
return isOK;
}
Upvotes: 0
Views: 265
Reputation: 1
you should write define _this = this;
in head, then replace all this
of _this
Upvotes: 0
Reputation: 12960
Create validationInfo as an Object not an Array
var validationInfo = {step1: [], step2: [] };
validationInfo['step1'].push("haha");
validationInfo['step2'].push("hehe");
console.log(validationInfo)
In your loop, I believe you have to do similar change, for example:
validationInfo[stepId][i].elementId
assuming stepId
is one of step1
or step2
. Wen you access validationInfo[i]
, you are accessing keys like: 0, 1 ,2, etc
in the validationInfo
. Those are the indexes of your step1
or step2
arrays..
Upvotes: 1
Reputation: 42
As you can not pass the key when you use push method in javascript, you can try this.
validationInfo.push({ value: { elementId: "txtsearchBar", validateFunc: isEmpty }, index: 'step1'});
Upvotes: 0