What could be causing - Uncaught TypeError: validOptions[i].toLowerCase is not a function?
-
16-10-2019 - |
Question
I am trying to add some additional jQuery functionality to Magento but my script fails to run and in the browser console I notice this error:
Uncaught TypeError: Object function each(iterator, context) {
var index = 0;
try {
this._each(function(value) {
iterator.call(context, value, index++);
});
} catch (e) {
if (e != $break) throw e;
}
return this;
} has no method 'toLowerCase'
Here is my script
<script>
jQuery(function() {
jQuery('.temp-variable-price').on('input', function() {
if(jQuery('input[class=temp-variable-price]').val()=="free")
{
jQuery('.temp-variable-price').val('FREE');
}
});
var validOptions = [];
validOptions.push("FREE");
var x = 1;
while (x<1000) {
if (x<100) {
validOptions.push(""+x+"p");
} else {
var y = x.toString();
var z = y.split('');
validOptions.push(""+z[0]+"."+z[1]+""+z[2]+"");
}
x++;
}
previousValue = "";
jQuery('.temp-variable-price').autocomplete({
autoFocus: true,
source: validOptions
}).keyup(function() {
var isValid = false;
for (i in validOptions) {
if (validOptions[i].toLowerCase().match(this.value.toLowerCase())) {
isValid = true;
}
}
if (!isValid) {
this.value = previousValue
} else {
previousValue = this.value;
}
});
});
</script>
Its nice autocomplete and validate script for certain form fields, you can see a demo here
If anybody knows what this error means I'd be so grateful for an explanation of how to debug and fix this issue?
Solution
It means validOptions[i]
is not a string. Use console.log(validOptions[i]);
to find out what it is. It might be undefined
.
Optionally you can do the following to ensure it's always a string
validOptions[i].toString().toLowerCase()....
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange