I can do something like this:
validator.showErrors({ "nameOfField" : "ErrorMessage" });
And that works fine, however if I try and do something like this:
var propertyName = "nameOfField";
var errorMessage = "ErrorMessage";
validator.showErrors({ propertyName : errorMessage });
It throws an 'element is undefined' error.
What about:
var propertyName = "nameOfField";
var errorMessage = "ErrorMessage";
var obj = new Object();
obj[propertyName] = errorMessage;
validator.showErrors(obj);
Is worth to notice that the following three syntaxes are equivalent:
var a = {'a':0, 'b':1, 'c':2};
var b = new Object();
b['a'] = 0;
b['b'] = 1;
b['c'] = 2;
var c = new Object();
c.a = 0;
c.b = 1;
c.c = 2;
The reason you're getting an 'element is undefined' error there by the way, is because:
var propertyName = "test";
var a = {propertyName: "test"};
is equivalent to..
var a = {"propertyName": "test"};
i.e., you're not assigning the value of propertyName as the key, you're assigning propertyName as a string to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With